GCJ 2008 - Alien Numbers

'Alien Numbers' problem statement:


The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as "oF8", then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that's written in one alien system into a second alien system.


The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

alien_number source_language target_language

Each language will be represented by a list of its digits, ordered from lowest to highest value. No digit will be repeated in any representation, all digits in the alien number will be present in the source language, and the first digit of the alien number will not be the lowest valued digit of the source language (in other words, the alien numbers have no leading zeroes). Each digit will either be a number 0-9, an uppercase or lowercase letter, or one of the following symbols !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~


For each test case, output one line containing "Case #x: " followed by the alien number translated from the source language to the target language.


1 ≤ N ≤ 100.

Small dataset

1 ≤ num digits in alien_number ≤ 4,
2 ≤ num digits in source_language ≤ 16,
2 ≤ num digits in target_language ≤ 16.

Large dataset

1 ≤ alien_number (in decimal) ≤ 1000000000,
2 ≤ num digits in source_language ≤ 94,
2 ≤ num digits in target_language ≤ 94.

Sample Input

9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01

Sample Output

Case #1: Foo
Case #2: 9
Case #3: 10011
Case #4: JAM!

There are a couple of ways you can code a solution for this, but if you know (or did some research on) number systems, you can figure out the easiest solution.

My Solution:

Since the 'decimal system' is the easiest that we humans and the programming languages (Higher-Level Languages) can comprehend, I converted the 'Alien Number' (using 'Source Language' input) into a decimal number and then divided that number with the 'base of the 'Target Language' number system) . The remainder is found and if its not zero, the division has to be done repeatedly on the quotient of the previous division until the remainder is '0' and collect all the 'remainders'. Form a number from the remainders, in the reverse order....and....VOILA!

Example: (Converting '19' (decimal) to 'base 5')

19/5 = 3 ------> 4 (remainder)
3/5 = 0 - -----> 3 (remainder)

So, the decimal number 19 is equal to 34 in 'base 5'. Simple! :-)

Then I uploaded the output files and found the solution to be 'Correct'.

Here's the code for my solution:

English To 'Pig Latin'

Yesterday, I was doing some research about 'Latin' and 'Roman Civilization' and as I was scouring the internet, I stumbled upon this slang (or 'Backslang') called 'Pig Latin', which is also a 'English Language Game'. Following are the rules to translate a 'English' word into 'Pig Latin':

1) For words that begin with 'consonant' sounds, move the initial consonant or consonant cluster to the end of the word and add "ay."

Ex: English: Zeitgeist ------ Pig Latin: eitgeistzay

2) For words that begin with vowel sounds (including silent consonants), simply add the syllable "ay" to the end of the word.

Ex: Englsih: Algorithm ------ Pig Latin: Algorithmway

This is the basic stuff for 'Pig Latin'. If you wanna know more about this, you can fire a Google query and do some research. Also, I'm not covering much of 'silent consonants' stuff in this post.

Ok..I thought its pretty simple and wrote a basic 'Java' program to translate an 'English phrase' into 'Pig Latin'. This is a pretty basic 'Java' program tested with just a couple of test cases. And ofcourse, there are a lot of other things to be added, in order to make it a perfect translator. I'm just posting the 'Java' code that I wrote in a couple of minutes, outta my own interest. Here's the code:

import java.util.Scanner;

public class EnglishToPigLatinTranslator {
public static void main(String[] args) {
System.out.println("Enter The English Phrase To Be " 
+ "Translated Into 'Pig Latin' : ");
Scanner scanner = new Scanner(System.in);

String strEngPhrase = scanner.nextLine();

if(strEngPhrase != null && !strEngPhrase.equals("")) {
System.out.println("\nPig Latin Text : \n" + 
} else {
System.out.println("No Input Specified!");

public static String convertEnglishToPigLatin(String strEnglishPhrase) {
String strVowels = "aeiou";
String[] strTokens = strEnglishPhrase.split("[ ]");
StringBuffer sbPigLatinStuff = new StringBuffer();

for(int i=0;i<strTokens.length;i++) {
if(strVowels.indexOf(strTokens[i].charAt(0)) >= 0) {
sbPigLatinStuff.append(strTokens[i] + "way ");
} else if((strTokens[i].indexOf("a") < 0) && 
(strTokens[i].indexOf("e") < 0) && 
(strTokens[i].indexOf("i") < 0) && 
(strTokens[i].indexOf("o") < 0) && 
(strTokens[i].indexOf("u") < 0)) {
sbPigLatinStuff.append(strTokens[i] + "ay ");          
} else {
for(int j=1;j<strTokens[i].length();j++) {
if(strVowels.indexOf(strTokens[i].charAt(j)) >= 0) {
sbPigLatinStuff.append(strTokens[i].substring(j) + 
strTokens[i].substring(0,j) + "ay ");

return sbPigLatinStuff.toString();

The above code reads/takes 'English' phrase as input from the console and outputs a 'Pig Latin' phrase as output.

Example Input: She sells sea shells by the sea shore
Output: eShay ellssay easay ellsshay byay ethay easay oreshay

Also, the above code doesn't take care of special characters. If you want, you can customize that, accordingly. Here's an input phrase, which shows that:

Input: A skunk sat on a stump and thunk the stump stunk, but the stump thunk the skunk stunk.

Output: Aay unkskay atsay onway away umpstay andway unkthay ethay umpstay unk,stay utbay ethay umpstay unkthay ethay unkskay unk.stay

One more interesting thing is that, Google also offers its search in 'Pig Latin'. I used the link and searched using a set of keywords and found that this 'Pig Latin' search gives priority to 'Pig Latin' stuff only if you search in 'Pig Latin'. Otherwise, it is just the usual way of 'Googling'. I dont know if they cover all the cases, but here's the link:

Google Igpay Atinlay (Pig Latin)

Playing With Googol

Sample program, which shows how to deal with large numbers like Googol:

Here's the output of this program:

Googol1 : 9223372036854775807
Max Value of Long : 9223372036854775807
Googol2 : 1.0E+100
Googol Multiplied By PI : 3.1415926535897930E+100

Google Billboard Puzzle

In July 2004, Google tried a innovative way of recruiting talented people, by posting a mathematical puzzle on a billboard on 'Highway 101' in the heart of silicon valley. The billboard read:

{first 10-digit prime found in consecutive digits e}.com."

The answer, 7427466391.com, would lead to a Web page with yet another equation to solve, with still no sign of the firm that wanted to recruit some of the best math guys, out there. It wasn't known that it was Google, until the puzzle was cracked.

Here's a pic of that billbaord:

I got to know this puzzle much later and thought of giving it a try. Without thinking much, my first idea was to use a simple, naive, brute-force approach of calculating Euler's Number (e) to around 10000 digits and then loop over the decimal part of 'e', taking 10 digits in each step and incrementing by 1 digit, in each step. I tried it and it worked, without consuming much time.

"Euler's Number" can be calculated in a couple of ways, but I used the following formula:

With the above formula, we can calculate 'e' by looping through, finding factorial and adding up 1/Facorial(n), in each and every loop. Java was my programming language of choice, but calculating e to 10000 digits precision is a problem because you cant use the normal primitives like "double", "float" or whatever to contain the huge number of digits in the decimal part of a number. So, it was pretty obvious that I had to use 'BigDecimal'.

One more tricky part was to determine 'how many times should the program loop through , for the precision to be around 10000 digits'? I tried a couple of combinations and found that "100" would be good enough. Also it doesn't consume much time. If you try it for 10000 or more, it would probably take a couple of minutes or hours.

Ok....I looped through "100" times in the code, calculated 'e' to a bit more than 10000 digits, in the first step. The next step, I looped through the decimal part of 'e', taking 10 char substrings (starting from the decimal point) in each step and checking whether its a prime or not. If it's a prime, the Puzzle is solved!

Here's the Java code for finding the 'First 10 digit prime number in the consecutive digits of e':

I ran this program and got the following output:

First 10 digit prime number in the decimal part of e : 7427466391

So, thats the answer and the domain address would be 7427466391.com" (which is defunct now).

....and here's the "Euler's Number (e)" that I calculated using the above approach: 'e to 10000 digits'

PS: The above solution is a very basic, brute-force/trial-and-error approach that I used, but it very much solves the puzzle.

Euler's Number (e) to 10000 digits

Below is the value of Euler's Number (e), up to 10000 digits: