Skip to content

Instantly share code, notes, and snippets.

@adprocas
Last active April 7, 2017 17:37
Show Gist options
  • Save adprocas/47b4154b34edbae4f84d05aaf12552e1 to your computer and use it in GitHub Desktop.
Save adprocas/47b4154b34edbae4f84d05aaf12552e1 to your computer and use it in GitHub Desktop.
ProjectEulerProblem17
package com.company.ProjectEuler;
/**
* Created by Adam Leathorn on 07/04/2017.
*/
public class ProjectEulerProblem17
{
String[] numberNames;
public ProjectEulerProblem17() {
numberNames = new String[1001];
populateArray();
long count = 0;
for(int x = 0; x < 1001; x++) {
count += numberNames[x].length();
}
System.out.println("number is: " + count);
}
public void populateArray() {
int x = 0;
numberNames[x++] = "";
numberNames[x++] = "one";
numberNames[x++] = "two";
numberNames[x++] = "three";
numberNames[x++] = "four";
numberNames[x++] = "five";
numberNames[x++] = "six";
numberNames[x++] = "seven";
numberNames[x++] = "eight";
numberNames[x++] = "nine";
numberNames[x++] = "ten";
numberNames[x++] = "eleven";
numberNames[x++] = "twelve";
numberNames[x++] = "thirteen";
numberNames[x++] = "fourteen";
numberNames[x++] = "fifteen";
numberNames[x++] = "sixteen";
numberNames[x++] = "seventeen";
numberNames[x++] = "eighteen";
numberNames[x++] = "nineteen";
numberNames[x++] = "twenty";
int tmp = x + 9;
int last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "thirty";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "forty";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "fifty";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "sixty";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "seventy";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "eighty";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
numberNames[x++] = "ninety";
tmp = x + 9;
last = x-1;
x = doNextTenSet(x, tmp, last);
x = doHundredSets(x);
numberNames[x++] = "onethousand";
}
public int doNextTenSet(int x, int tmp, int last) {
while(x < tmp) {
int minusNum = x / 10;
int indexNum = x - (minusNum*10);
numberNames[x++] = numberNames[last] + numberNames[indexNum];
}
return x;
}
public int doHundredSets(int x) {
while(x < 1000) {
int bigNum = x / 100;
int littleNum = x - (bigNum*100);
if(littleNum == 0)
numberNames[x++] = numberNames[bigNum] + "hundred";
else
numberNames[x++] = numberNames[bigNum] + "hundredand" + numberNames[littleNum];
}
return x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment