base 13 - The Hitchhiker's Guide to the Galaxy [closed] - binary

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I know base 13 is not practical. However, i was checking out The Hitchhiker's Guide to the Galaxy again because todays date is 10/10/10 = 42 in binary. (you know, the answer to the ultimate question of life, the universe, and everything) It takes Deep Thought 7½ million years to compute and check the answer, which turns out to be 42. Unfortunately, The Ultimate Question itself is unknown. Anyways, he said ""Six by nine. Forty two." "That's it. That's all there is.""I always thought something was fundamentally wrong with the universe"
My question is how is 6 x 9 in base 13 = 42?
I know how to convert base 10, 2,16,8 but going from base 10 to base 13 is a mystery to me.
I understand that in base 13 that 6 probably =6 and 9 probably =9 S0, it is just a matter of multiplying in base 13?
Can someone work it out?
I have found this but doesnt help much
in base 13, 6 13 × 9 13 is actually 4213
(as 4 × 13 + 2 = 54, i.e. 54 in decimal is
equal to 42 expressed in base 13).

This is a method that can convert a base-10 number to base-13:
Start with a number 9x6=54, we want to find the equivalent of 54 in base 13.
54 / 13 = 4 remainder 2
4 / 13 = 0 remainder 4
and we concatenate the remainders, from bottom-up, 42.
A more general algorithm, start with a decimal number N, we want to find the equivalent of N in base B.
N / B = a1 remainder r1
a1 / B = a2 remainder r2
....
an / B = 0 remainder rn
and concatenate the digits, bottom-up: rn . ... . r2 . r1
an iterative implementation in Python:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(n, base=2):
''' Convert a positive integer to number string with base `base` '''
s = []
while n != 0:
n, rem = divmod(n, base)
s.append(digits[rem])
return ''.join(reversed(s))
a recursive implementation in Python:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(n, base=2):
''' Convert a positive integer to number string with base `base` '''
return rebase(n // base, base) + digits[n % base] if n != 0 else ''
even more generally, if you have a string representing a number in base N and you want to convert it to a string representing the number in base M:
digits = '0123456789abcdefghijklmnopqrstuvwxyz'
def rebase(orig, orig_base=10, target_base=2):
''' Convert a positive integer to number string with base `base` '''
num = 0
for i, n in enumerate(reversed(orig)):
num += digits.find(n) * (orig_base**i)
target = []
while num != 0:
num, rem = divmod(num, target_base)
target.append(digits[rem])
return ''.join(reversed(target))

It's not a matter of multplying a number in a different base, but about expressing the product in that base
Lets start with a really easy base, unary, which is expressed in only ones (not even zeroes)
6x9 in unary is 111111 x 111111111. we can perform that calculation by replacing all of the ones in one term with the ones in the other term. copy and paste the nine ones six times
111111111111111111111111111111111111111111111111111111
When we want to express this number in more convenient bases, we group the ones by the radix. If there are enough groups to group the groups, we group them. we then replace the counts of groups with digits. We'll do that in decimal
111111111111111111111111111111111111111111111111111111
^ ^ ^ ^ ^
Each arrow is a group of 10, and there are 4 ones left over, so in the tens place, we put a 5 and in the ones place a 4, 54.
lets do the same for a smaller base so we can get a good idea how to generalize the groups
of groups:
1 111111111111111111111111111111111111111111111111111111
2 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
4 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8 ^ ^ ^ ^ ^ ^
16 ^ ^ ^
32 ^
We could make groups five times. starting in the ones place, there we no left over ones after we grouped by two, so the first digit is 0. when we grouped by 4, there was still a group of 2 left over, so the next digit is a 1. when we grouped by 8, there was still a group of 4 left over, another 1 is the next digit. when we grouped by 16, there was one remaining group of 8. when grouping by 32, there's a group of 16 left. we can't make a group of anything as large as 64, so all of the digits for places above 32 are 0. so the binary representation would be
110110
finally, base 13. this is just as easy as the base 10
111111111111111111111111111111111111111111111111111111
^ ^ ^ ^
there are 4 groups of 13. there are two digits left over after we make those 4 groups. thus the product of 6 x 9, when represented in base 13 is '42'

The Answer 42 has nothing to do with base 13, it was just a computational error.
The answer to this is very simple. It
was a joke. It had to be a number, an
ordinary, smallish number, and I chose
that one. Binary representations, base
thirteen, Tibetan monks are all
complete nonsense. I sat at my desk,
stared into the garden and thought '42
will do'. I typed it out. End of
story.
http://en.wikipedia.org/wiki/Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29

Related

How to differenciate this two string with a REGEX?

I am trying to identify Spanish ID numbers using REGEX on MySQL. I am took this regex to adapt it to my dataset, as the items are not isolated and might not start/end with those characters. The expressions are:
Original: ^(x?\d{8}|[xyz]\d{7})[trwagmyfpdxbnjzsqvhlcke]$
Mine:[0-9]{8,8}[A-Za-z]{1}
When I run the search using my REGEX, this is a sample of what I get:
GOOD --> 47099085T
GOOD --> D73654109H
NOT OK --> 8.30781719e-05
NOT OK --> 0113:11:19%2000:54:17.042828927Z
How can I modify [0-9]{8,8}[A-Za-z]{1} to exclude the "NOT OK" items?
Spanish ID syntax:
The number of the National Identity Document includes 8 digits and one letter for security. The letter is found by taking all 8 digits as a number and dividing it by 23. The remainder of this digit, which is between 0 and 22, gives the letter used for security. The letters I, Ñ, O, U are not used. The letters I and O are not used – to avoid confusions with the numbers 0 and 1. The Ñ is not used to avoid confusions with N.
Remainder: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Letter: T R W A G M Y F P D X B N J Z S Q V H L C K E
-- EDIT II --
After running a test on a bigger data set, I have found other matches that should be excluded.
How can I modify (^|[^0-9.])([0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke]) to DO NOT match:
70ce4827ce88530583ed5a1a40245f24
BE4-SGS-V2-00199982a5aa
2945a6bf-86b6-4ea0-94d9-aec84980762d
0x01010083B5627CCA663946A282DE573804AA85
xmp.iid:FE7F11740720681189A59382544B2855
Ok, according to documentation the Spanish ID system (DNI) is structured thus:
The number of the National Identity Document includes 8 digits and one letter for security. The letter is found by taking all 8 digits as a number and dividing it by 23. The remainder of this digit, which is between 0 and 22, gives the letter used for security. The letters I, Ñ, O, U are not used. The letters I and O are not used – to avoid confusions with the numbers 0 and 1. The Ñ is not used to avoid confusions with N.
Remainder: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Letter: T R W A G M Y F P D X B N J Z S Q V H L C K E
After some exploration with Negative Lookaheads and completely failing to get them to work, we can use a more manual approach to a solution, by manually checking that the found "block" of 8 integers is not preceeded by an integer or a decimal point:
/[^\.\d][\d]{8}[TRWAGMYFPDXBNJZSQVHLCKE]/gmi
MySQL safe/syntax version:
(^|[^0-9.])([0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])
Example usage using REGEX_REPLACE to return rows where the id_column matches the ID syntax and returns those syntax strings:
SELECT REGEXP_REPLACE(`id_column`,
'(^|[^\\d.])(\\d{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])', '$2') as id_output
FROM `table_name`
WHERE id_column REGEXP '(^|[^\\d.])(\\d{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])'
NOTE: Prior to MySQL 8.0.17, the result returned by this function used the UTF-16 character set; in MySQL 8.0.17 and later, the character set and collation of the expression searched for matches is used. (Bug #94203, Bug #29308212)
This matches the two correct matches on your example as well as checking that only one of the valid letters comes after the numerical match.
It is important to note that using the max value in the qualifier {min,max} is pretty irrelevant because it does not mean no more than max should exist in the source string. Please see here for further reading.
What does my Regex do:
Checks that a set of 8 integers is not preceeded by either another integer or a decimal point (so 9 integers are never "captured").
Checks that the set of 8 found integers is immediately followed by one of the valid letters of either case.
You can see my Regex in action here and the corresponding MySQL demo here.
47099085T // matches
D73654109H // matches
8.30781719e-05 // unmatched
0113:11:19%2000:54:17.042828927Z // unmatched

How do I get the b-complement of 193 for b = 5?

So I am supposed to find the b-complement for 193 given that b = 5 and the number of digits allowed is n = 6.
Now I think I am a bit confused regarding the b-complement in general.
Is it that whenever b is odd, just like in the case we have here, you are supposed to subtract 1 from the base and calculate the complement with the result?
If that's the case, you would calculate the 4-complement of 193, right?
But do I then also subtract the then found out number from 444444 or still 555555?

Binary digits in decimal

How do you represent (decimal) integer 50 in binary?
How many bits must be "flipped" in order to capitalize a lowercase 'a' that is represented in ASC11?
How do you represent the (decimal) integer 50 in, oh, "hexadecimal," otherwise known as base-16? Recall that decimal is simply base-10, and binary is simply base-2. Infer from those base systems how to represent this one?
Please answer these questions for me.HELP.
To help you some:
Binary is only made up of 1's and 0's.This may help you understand binary conversion
Decimal is 0-9
Hexadecimal is 0-9, then A-F (so A would represent 10, B would be 11, etc up to F which is 15)
Converting from decimal to another base
Here some tips for you regarding conversion to binary:
What is 50 mod 2? What about 25 mod 2 and then 12 mod 2? What are your results if you continue this?
What does any number mod 2 (always) return as result? - 1 or 0
Do you realise any patterns? - You get the reversed binary number as result
Test case 50:
50 mod 2 = 0 - 6th digit
25 mod 2 = 1 - 5th digit
12 mod 2 = 0 - 4th digit
6 mod 2 = 0 - 3rd digit
3 mod 2 = 1 - 2nd digit
1 mod 2 = 1 - 1st digit
The remainders of the divisions concatenated and reverses are: 110010, which is 50 in binary.
Can this be also transformed to further bases? - Yes, as we see with trying to convert 50 to hexadecimal:
50 mod 16 = 2 - 2nd digit
3 mod 16 = 3 - 1st digit
The remainders again concatenated and reversed are 32, which conveniently is 50 in hexadecimal.
In general we can say to convert a number to an arbitrary base you must take the remainder of the number and the base and then divide the number by the base and do the same thing again. In a program this would look something like:
while the number is greater 0 do:
result = (number mod base) + result;
number = number div base;
Converting from any base to decimal
How do you convert a number from an arbitrary base into base 10? First let us do a test case with binary. Lets take the 50 from the previous example: 110010
The method to convert from binary is multiplying every digit with the base to the power of the position of it in the number and adding up the result. The enumeration of the positions begins with 0 at the least significant digit. Our previous number would then look something like this:
1 *2^5 + 1 *2^4 + 0 *2^3 + 0 *2^2 + 1 *2^1 + 0 *2^0
What simplifies to:
32 + 16 + 2 = 50
It also works with any other base, like our 32 from the previous example:
3 *16^1 + 2*16^0 = 48 + 2 = 50
In program this would look something like this:
from end of number to beginning do:
result = result + digit * (base ^ position)

How Does Modulus Divison Work

I don't really understand how modulus division works.
I was calculating 27 % 16 and wound up with 11 and I don't understand why.
I can't seem to find an explanation in layman's terms online.
Can someone elaborate on a very high level as to what's going on here?
Most explanations miss one important step, let's fill the gap using another example.
Given the following:
Dividend: 16
Divisor: 6
The modulus function looks like this:
16 % 6 = 4
Let's determine why this is.
First, perform integer division, which is similar to normal division, except any fractional number (a.k.a. remainder) is discarded:
16 / 6 = 2
Then, multiply the result of the above division (2) with our divisor (6):
2 * 6 = 12
Finally, subtract the result of the above multiplication (12) from our dividend (16):
16 - 12 = 4
The result of this subtraction, 4, the remainder, is the same result of our modulus above!
The result of a modulo division is the remainder of an integer division of the given numbers.
That means:
27 / 16 = 1, remainder 11
=> 27 mod 16 = 11
Other examples:
30 / 3 = 10, remainder 0
=> 30 mod 3 = 0
35 / 3 = 11, remainder 2
=> 35 mod 3 = 2
The simple formula for calculating modulus is :-
[Dividend-{(Dividend/Divisor)*Divisor}]
So, 27 % 16 :-
27- {(27/16)*16}
27-{1*16}
Answer= 11
Note:
All calculations are with integers. In case of a decimal quotient, the part after the decimal is to be ignored/truncated.
eg: 27/16= 1.6875 is to be taken as just 1 in the above mentioned formula. 0.6875 is ignored.
Compilers of computer languages treat an integer with decimal part the same way (by truncating after the decimal) as well
Maybe the example with an clock could help you understand the modulo.
A familiar use of modular arithmetic is its use in the 12-hour clock, in which the day is divided into two 12 hour periods.
Lets say we have currently this time: 15:00
But you could also say it is 3 pm
This is exactly what modulo does:
15 / 12 = 1, remainder 3
You find this example better explained on wikipedia: Wikipedia Modulo Article
The modulus operator takes a division statement and returns whatever is left over from that calculation, the "remaining" data, so to speak, such as 13 / 5 = 2. Which means, there is 3 left over, or remaining from that calculation. Why? because 2 * 5 = 10. Thus, 13 - 10 = 3.
The modulus operator does all that calculation for you, 13 % 5 = 3.
modulus division is simply this : divide two numbers and return the remainder only
27 / 16 = 1 with 11 left over, therefore 27 % 16 = 11
ditto 43 / 16 = 2 with 11 left over so 43 % 16 = 11 too
Very simple: a % b is defined as the remainder of the division of a by b.
See the wikipedia article for more examples.
I would like to add one more thing:
it's easy to calculate modulo when dividend is greater/larger than divisor
dividend = 5
divisor = 3
5 % 3 = 2
3)5(1
3
-----
2
but what if divisor is smaller than dividend
dividend = 3
divisor = 5
3 % 5 = 3 ?? how
This is because, since 5 cannot divide 3 directly, modulo will be what dividend is
I hope these simple steps will help:
20 % 3 = 2
20 / 3 = 6; do not include the .6667 – just ignore it
3 * 6 = 18
20 - 18 = 2, which is the remainder of the modulo
Easier when your number after the decimal (0.xxx) is short. Then all you need to do is multiply that number with the number after the division.
Ex: 32 % 12 = 8
You do 32/12=2.666666667
Then you throw the 2 away, and focus on the 0.666666667
0.666666667*12=8 <-- That's your answer.
(again, only easy when the number after the decimal is short)
27 % 16 = 11
You can interpret it this way:
16 goes 1 time into 27 before passing it.
16 * 2 = 32.
So you could say that 16 goes one time in 27 with a remainder of 11.
In fact,
16 + 11 = 27
An other exemple:
20 % 3 = 2
Well 3 goes 6 times into 20 before passing it.
3 * 6 = 18
To add-up to 20 we need 2 so the remainder of the modulus expression is 2.
The only important thing to understand is that modulus (denoted here by % like in C) is defined through the Euclidean division.
For any two (d, q) integers the following is always true:
d = ( d / q ) * q + ( d % q )
As you can see the value of d%q depends on the value of d/q. Generally for positive integers d/q is truncated toward zero, for instance 5/2 gives 2, hence:
5 = (5/2)*2 + (5%2) => 5 = 2*2 + (5%2) => 5%2 = 1
However for negative integers the situation is less clear and depends on the language and/or the standard. For instance -5/2 can return -2 (truncated toward zero as before) but can also returns -3 (with another language).
In the first case:
-5 = (-5/2)*2 + (-5%2) => -5 = -2*2 + (-5%2) => -5%2 = -1
but in the second one:
-5 = (-5/2)*2 + (-5%2) => -5 = -3*2 + (-5%2) => -5%2 = +1
As said before, just remember the invariant, which is the Euclidean division.
Further details:
What is the behavior of integer division?
Division and Modulus for Computer Scientists
Modulus division gives you the remainder of a division, rather than the quotient.
It's simple, Modulus operator(%) returns remainder after integer division. Let's take the example of your question. How 27 % 16 = 11? When you simply divide 27 by 16 i.e (27/16) then you get remainder as 11, and that is why your answer is 11.
Lets say you have 17 mod 6.
what total of 6 will get you the closest to 17, it will be 12 because if you go over 12 you will have 18 which is more that the question of 17 mod 6. You will then take 12 and minus from 17 which will give you your answer, in this case 5.
17 mod 6=5
Modulus division is pretty simple. It uses the remainder instead of the quotient.
1.0833... <-- Quotient
__
12|13
12
1 <-- Remainder
1.00 <-- Remainder can be used to find decimal values
.96
.040
.036
.0040 <-- remainder of 4 starts repeating here, so the quotient is 1.083333...
13/12 = 1R1, ergo 13%12 = 1.
It helps to think of modulus as a "cycle".
In other words, for the expression n % 12, the result will always be < 12.
That means the sequence for the set 0..100 for n % 12 is:
{0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,[...],4}
In that light, the modulus, as well as its uses, becomes much clearer.
Write out a table starting with 0.
{0,1,2,3,4}
Continue the table in rows.
{0,1,2,3,4}
{5,6,7,8,9}
{10,11,12,13,14}
Everything in column one is a multiple of 5. Everything in column 2 is a
multiple of 5 with 1 as a remainder. Now the abstract part: You can write
that (1) as 1/5 or as a decimal expansion. The modulus operator returns only
the column, or in another way of thinking, it returns the remainder on long
division. You are dealing in modulo(5). Different modulus, different table.
Think of a Hash Table.
When we divide two integers we will have an equation that looks like the following:
A/B​​ =Q remainder R
A is the dividend; B is the divisor; Q is the quotient and R is the remainder
Sometimes, we are only interested in what the remainder is when we divide A by B.
For these cases there is an operator called the modulo operator (abbreviated as mod).
Examples
16/5= 3 Remainder 1 i.e 16 Mod 5 is 1.
0/5= 0 Remainder 0 i.e 0 Mod 5 is 0.
-14/5= 3 Remainder 1 i.e. -14 Mod 5 is 1.
See Khan Academy Article for more information.
In Computer science, Hash table uses Mod operator to store the element where A will be the values after hashing, B will be the table size and R is the number of slots or key where element is inserted.
See How does a hash table works for more information
This was the best approach for me for understanding modulus operator. I will just explain to you through examples.
16 % 3
When you division these two number, remainder is the result. This is the way how i do it.
16 % 3 = 3 + 3 = 6; 6 + 3 = 9; 9 + 3 = 12; 12 + 3 = 15
So what is left to 16 is 1
16 % 3 = 1
Here is one more example: 16 % 7 = 7 + 7 = 14 what is left to 16? Is 2 16 % 7 = 2
One more: 24 % 6 = 6 + 6 = 12; 12 + 6 = 18; 18 + 6 = 24. So remainder is zero, 24 % 6 = 0

What is the "biggest" negative number on a 4-bit machine?

Or, what is the range of numbers that can be represented on a 4-bit machine using 2s-complement?
That would be -8 to +7
The range is -8 to 7, or 1000 to 0111. You can see the full range here.
4 bits (using 2's complement) will give you a range from -8 to 7.
This should be straightforward to work out yourself.
Range in twos complement will be:
-1 * 2 ^ (bits - 1)
to
2 ^ (bits - 1) - 1
So for 4 bits:
-1 * 2 ^ (4 - 1) = -1 * 2 ^ 3 = -8
to
2 ^ (4 - 1) - 1 = 2 ^ 3 - 1 = 7
Also, if you are interested and for others maybe browsing this question -
twos complement is used for easy binary arithmetic:
to add - you just add the two numbers without conversion and disregard the overflow:
-6 + 7 = 1
is
1010 = -6
0111 = 7
------
(1)0001 = 1 (ignoring the overflow)
...and more yet - to convert a negative binary number to its opposite positive number:
if the sign bit (highest order bit) is 1, indicating negative... reading from least significant to most significant bit (right to left), preserve every bit up through the first "1", then invert every bit after that.
So, with 8 bit
10011000 .. becomes
01101000 (* -1) = 104 * -1 = -104
and that is why 10000000 is your lowest negative number (or in X bit 1000.all zeroes..000), it translates to unsigned 10000000 * -1 = -128
Maybe a long winded answer but to those without the 1s and 0s background I figure it is useful
Well let's dissect this question.
Firstly, the question should be framed as - "The least*(because it is negative, so biggest is not the right usage)* possible negative number to be stored in 4-bit would be?"
Numbers are of two types -
Signed (holds both + and - numbers )
Unsigned (holds only + numbers)
We will be using binary representation to understand this.
For Unsigned -
4-bit minimum number = 0000 (0)
4-bit maximum number = 1111 (255)
So range would be Range : 0-15
For Signed 4-bits, first bit represent sign(+/-) and rest 3-bits represent number.
4-bit minimum will be a negative number.
So replace the first bit(MSB) with 1 in 0000(least unsigned number), making it 1000.
Calculate decimal equivalent of 1000 = 1*2^3 = 8
So, number would be -8 (- as we have 1 as the first bit in 1000)
4-bit maximum will be a positive number.
So replace the first bit(MSB) with 0 in 1111(largest unsigned number), making it 0111.
Calculate decimal equivalent of 0111 = 1*2^2 + 1*2^1 + 1*2^0 = 7
So, number would be +7 (+ as we have 0 as the first bit in 0111)
Range would be -8 to +7.