Translating anything into a number using math

codecnomad1 pts0 comments

Ever thought about how you can translate anything into a number using math? — codecnomad

Ever thought about how you can translate anything into a number using math?<br>A few things which we need to understand before we can translate into a number

Primes are the solution to our problem. A number bigger than 1, which has no other divisors than itself and 1.

Why is this great for us? If we multiply 2 primes together, we'll be able to divide it with those primes and know how many times the number contains either of our primes. This also means that we can check if the final number was created by multiplying x and y and z primes, or just x and z.

As a result of this , we can now create unique “objects,” and now we can check if they exist.

For example : Let's say that we want to encode if our friend Nate has a pen, a pencil, or both. We know that a pen and a pencil are unique objects, so let's just make a pen 2 and let's map the pencil to the number 3. If 2 exists, that means he has a pen, the same goes for 3. This means that 2 * 3 would mean that he has both, since both numbers exist, which means we have 6.

How would we check if he has a pen? Simply check if 6 can be divided by 2, in our case, yes, once. This means that he has a pen.

What if he has a pencil but no pen? 3 would be our answer. This is simplified from 1 * 3, but where does the 1 come from? 2^0 * 3.

But this is also a simplification of 2^0 * 3^1. This quite literally means 0 of pen and 1 of pencil. We can obviously now encode how many of those items we have.

Now we can count items, but we can't really create classes to actually describe what these items are... or can we?

Let's say that we want to create a class that has item count, that has color, and has durability. We do this with recursion. Here's how you'd create it.

Pseudo code:

class item:<br>item_count: integer<br>color: integer (1=yellow, 2=green, 3=blue)<br>durability: integer (between 0, 10 where 10 presents 100%)

[item(3, 2, 4), item(1, 3, 1)]

Using math:

2 ^ [(2^3) * (3^2) * (5^4)] * 3 ^ [(2^1) * (3^3) * (5^1)]

This means that primes are used as “unique objects” that represent something, whilst the power they're on represents our actual data.

x^[(2^3) * (3^2) * (5^4)] → x represents a unique object, aka our item

2^[(f^y) * (3^2) * (5^4)] → f represents the unique field, aka the item_count field, whilst y represents the value of the field

Notes

The conversion is lossless as long as you keep the schema we used to convert our data. However, the number size blows out of proportion the more you encode, so it isn't advisable to use in most places in computer science.

Also, you can encode negative numbers by doing a zigzag-style pattern: outside_value -> encoded_in_power, 0 -> 0, 1 -> 1, (-1) -> 2...

I've read a bit about Gödel numbering a while ago, and that's what kinda inspired me to make this.

number means primes unique pencil item

Related Articles