[2015-11-02] Challenge #239 [Easy] A Game of Threes : dailyprogrammer
Back in middle school, I had a peculiar way of dealing with super boring classes. I would take my handy pocket calculator and play a "Game of Threes". Here's how you play it:
First, you mash in a random large number to start with. Then, repeatedly do the following:
While the game was originally a race against myself in order to hone quick math reflexes, it also poses an opportunity for some interesting programming challenges. Today, the challenge is to create a program that "plays" the Game of Threes.
public static void main(String[] args) {
int input = 31337357;
while(input > 1) {
int n = input % 3 == 0 ? 0 : input % 3 == 1 ? -1 : 1;
System.out.println(input + " " + n);
input = (input + n) / 3;
}
System.out.println(input);
}
Read full article from [2015-11-02] Challenge #239 [Easy] A Game of Threes : dailyprogrammer
Back in middle school, I had a peculiar way of dealing with super boring classes. I would take my handy pocket calculator and play a "Game of Threes". Here's how you play it:
First, you mash in a random large number to start with. Then, repeatedly do the following:
- If the number is divisible by 3, divide it by 3.
- If it's not, either add 1 or subtract 1 (to make it divisible by 3), then divide it by 3.
While the game was originally a race against myself in order to hone quick math reflexes, it also poses an opportunity for some interesting programming challenges. Today, the challenge is to create a program that "plays" the Game of Threes.
public static void main(String[] args) {
int input = 31337357;
while(input > 1) {
int n = input % 3 == 0 ? 0 : input % 3 == 1 ? -1 : 1;
System.out.println(input + " " + n);
input = (input + n) / 3;
}
System.out.println(input);
}
Read full article from [2015-11-02] Challenge #239 [Easy] A Game of Threes : dailyprogrammer