Created
November 6, 2016 20:58
-
-
Save khayyamsaleem/68a94d95b96024a885016d791cc7aee9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class GCD{ | |
public static int gcd(int a, int b){ | |
if (b==0) return a; | |
return gcd(b, a%b); | |
}; | |
public static void main(String[] args){ | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Enter a number:"); | |
int x = scanner.nextInt(); | |
ArrayList< ArrayList<Integer> > grid = new ArrayList< ArrayList<Integer> >(); | |
ArrayList<Integer> answers = new ArrayList<Integer>(); | |
for (int i = 1; i <= x; i++){ | |
for (int j=1; j <=x; j++ ){ | |
ArrayList<Integer> inner = new ArrayList<Integer>(); | |
inner.add(i); | |
inner.add(j); | |
grid.add(inner); | |
}; | |
}; | |
for (int i = 0; i < grid.size(); i++){ | |
answers.add(gcd(grid.get(i).get(0), grid.get(i).get(1))); | |
} | |
System.out.print(" "); | |
for (int i=1; i<=x; i++){ | |
System.out.print(i); | |
System.out.print(" "); | |
} | |
System.out.println(); | |
int w = 1; | |
while(answers.size() != 0){ | |
System.out.print(w); | |
System.out.print(" "); | |
for (int j = 0; j < x; j++){ | |
System.out.print(answers.get(0)); | |
System.out.print(" "); | |
answers.remove(0); | |
} | |
w++; | |
System.out.println(); | |
} | |
//System.out.println(answers); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment