# More number theory

• January 12th, 2012, 02:17 PM
JeremiahWalker
More number theory
Some code examples. I think I will focus on more useful and practical code from now on. I'm pretty sure noone cares about number theory. Can you guys give me a little insight as to how to structure and guide my studies?

Code :

```//Jeremiah A. Walker //Euclidean algorithm to get the GCD(Greatest Common Denominator) of two numbers public class gcd { public static long Euclid(long a, long b) { if(b == 0)//if b is 0, return a return a; else//if b\ is not 0 find GCD return Euclid(b, a % b); }//end Euclid   public static void main(String[] args) { long n0 = Long.parseLong(args[0]); long n1 = Long.parseLong(args[1]);   long res;   if (n0 > n1) res = Euclid(n0, n1); else if(n1 > n0) res = Euclid(n1, n0); else res = n0; System.out.println(res);   }//end main }//end gcd```

Code :

```//Jeremiah A. Walker //An integer is said to be a perfect number if its factors add up to the integer //For instance 6 is perfect because 1+2+3 =6 //Write a program that can determine if a number is perfect and print all the perfect numbers between 0 and x   import java.util.*;//scanner and arraylist   public class pnum { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Give me an integer between 2 and 10,000: ");//asks user for a number int number = input.nextInt();//reads user input int total = 0;//total, stores the sum of the factors of the user input number ArrayList<Integer> factor = new ArrayList<Integer>();   for(int i = 1; i < number; i++) { if(number % i == 0) { total += i; factor.add(i); }//end if }//end for System.out.print("1"); for(int i = 1; i < factor.size(); i++) { System.out.print(" +" + factor.get(i)); }//end for System.out.print(" = " + total + "\n"); if(total != number) System.out.print("This number is not perfect.\n"); else System.out.print("This number is perfect.\n"); }//end main }//end pnum```

Code :

```//Jeremiah A. Walker //Print all the perfect numbers from 2 to 10000 //Check each one import java.util.*;   public class pnum2 { public static void main(String[] args ) { for(int n = 2; n<=10000; n++) if(isPerfect(n)) System.out.println(n + " is perfect");   }//end main     private static boolean isPerfect(int n) { int total = 0; for(int i = 1; i < n; i++) if( n % i == 0) total += i; return (total == n); }//end isPerfect }//end class pnum2```
• January 13th, 2012, 12:49 AM
ChristopherLowe
Re: More number theory
If you like this kind of stuff, you will love Project Euler