Difficulty: ★ ★ ★ ☆
I watched the film Contact a few weeks ago. The movie is about first contact with an alien civilization. It’s smart cinema, which offers that communications between species would be mathematical in nature, as math is the universal language.
The topic of math as a language isn’t explored deeply in the picture, mostly because the filmmakers want the audience to stay awake. But recently during a pre-sleep moment, I wondered about math as a universal language. Yes, we could communicate with high-intelligent aliens by sending out prime numbers. But in what base?
In the film, the aliens transmit prime numbers as a series of pulses. Beyond this math, the other math in the film isn’t explained, though from screenshots it looks binary in nature. The puzzle I pondered was whether humans would assume that all intelligent species use base 10 decimal?
As a future employee of SETI, it’s your job to code a program that translates values from base 10 into another base. Don’t worry about communicating with aliens, this challenge is just pretend. In fact, I’m going to limit the bases from two to ten.
The program prompts for a base and then a value in decimal. The output shows the value as it appears in the other base. For example:
Enter base (>1 <11): 5
Enter value (>1): 12345
12345 in base 5 is 343340
The value 12,345 in base 5 is written as 343,340.
Here’s another run:
Enter base (>1 <11): 2
Enter value (>1): 85
85 in base 2 is 1010101
Your task is to write this program, perhaps not for SETI but as this month’s exercise.
As a bit of help, five years ago I wrote a post on outputting values in any base. But one of the keys for this exercise is for the result not to contain any leading zeros, as you see in the output above from my solution.
Try this exercise on your own before you check out my solution.
As the novel was written by Carl Sagan I assume it has a lot more detail than Hollywood would risk. The Wikipedia page https://en.wikipedia.org/wiki/Contact_(novel) mentions base 11 so you’ll need to edit your code! Maybe aliens have 3 fingers on one hand and 8 on the other.
Neat-o! I did not know this. I could update the code. I could do that . . .