This is a solution to @kenchung math and programming challenge:
@kenchung/question-mathematics-programming-competition-8#
which basically asks if make numbers in a digital clock type format (or standard 4 function calc) on a card, what is the least amount of cards needed-assuming you can turn the cards upside down-and not worry if the one is aligned left or right-up to the number 9999. Read the link to get a visual.
This is a very simple program.
Because we include "0000" or just "0", we have a total of 10000 numbers total.
And so I created a binary array of 10k binary objects. i just called the array data. I would ensure that all the values are set to false by running a for loop at the start of the main routine.
because it asks us a quantity, I needed to create a variable to count each unique solution and of course set it to zero.
I proceeded to create 4 for loops each creating a number between 0-9. This produces 4 digits.
I put the 4 digits together in a subroutine called "make" for form up to a 4 digit number number. This "make" subroutine is useful in both the forward case and the upsidedown case, which set up shortly.
We create a variable f, set to -1 which will ultimately point to an index in out data array representing the upside down case. But we first put out individual digits in a subroutine called "pos" to see if when turned upside down they are still numbers. If not we leave f at -1, if true we assign the upsidedown numbers to of by putting the reverse case of the numbers in make. you'll notice originally make(a,b,c,d) and in the reverse case make(trans(a),trans(c),trans(b),trans(a)) to keep things simple.
Now that we have our two data values, even if one value is illegal. We first see if our original number is already set to true in the data array. If it is true-we don't care about that case. If it is not true, it means we haven't encountered it yet so we increase our counter and we'll set that position to true.
We then do the same for the reverse. We ask if the reverse case f is greater than 0. (technically it should ask greater than or equal to, but the first value of e is 0 anyways). It also checks to make sure that f is unique for e. Perhaps that check was unnecessary, since we weren't going to count our f index anyways.
throughout the code we do print out a lot of data. This is to make sure there aren't off by one errors, and of course the print out of our final count value.
That pretty much is the end of the main routine.
The "make" subroutine is very simple, that it needs not further elaboration on.
the "trans"ition subroutine works under the assumption that everything can be turned upside down, the only two values that change are the 6 and the 9-and everything else is the same.
the "position" subroutine looks to see if any of the 4 input values has a number which can't be turned upside down. If it can't be turned upsidedown, it returns false. Otherwise it returns true.
import java.awt.*;
public class test2 {
public static boolean[] data = new boolean[10000];
public static int count=0;
public static void main(String args[]) {
for(int a=0;a<=9999;a++) {
data[a]=false;}
System.out.println(data[9999]);
for(int a=0;a<=9;a) {
for(int b=0;b<=9;b) {
for(int c=0;c<=9;c) {
for(int d=0;d<=9;d) {
int e=make(a,b,c,d);
int f=-1;
if (pos(a,b,c,d)) {f=make(trans(d),trans(c),trans(b),trans(a));}
if (!data[e]) {count++;data[e]=true;}
if (f>0 && e!=f) {data[f]=true;}
System.out.println(e+":"+f+":"+data[e]+":"+count);
}}}}
System.out.println(count);
}
public static boolean pos(int a, int b, int c, int d) {
if (a3 || a4 || a7 || b3 || b4 || b7 || c3 || c4 || c7 || d3 || d4 || d7) {return false;}
else {return true;}
}
public static int make(int a, int b, int c, int d) {return 1000*a+100*b+10*c+d;}
public static int trans(int a) {
if (a9) {return 6;}
else if (a6) {return 9;}
else {return a;}
}
}