Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: More number theory

1. ## 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?

```//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);
long n1 = Long.parseLong(args);

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```

```//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;
}//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```

```//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```  Reply With Quote

3. ## Re: More number theory

If you like this kind of stuff, you will love Project Euler  Reply With Quote