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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: More number theory

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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[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

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

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


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    305
    My Mood
    Bored
    Thanks
    45
    Thanked 85 Times in 71 Posts
    Blog Entries
    4

    Default Re: More number theory

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

Similar Threads

  1. Some help with Java theory?
    By Mudkipers in forum Java Theory & Questions
    Replies: 7
    Last Post: May 12th, 2011, 07:28 AM
  2. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  3. API singleton theory
    By dunnkers in forum Java Theory & Questions
    Replies: 10
    Last Post: February 24th, 2011, 11:57 AM
  4. Theory/Question,
    By Time in forum Java Theory & Questions
    Replies: 7
    Last Post: November 9th, 2010, 04:26 PM
  5. Theory Inquiry
    By b_jones10634 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 19th, 2010, 08:21 AM