LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-02-2003, 05:17 PM   #1
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Rep: Reputation: 16
on JAVA: finding perfect numbers


Hi all;

I learn by studying solved examples. This code checks for perfect numbers.

A natural number n is perfect if it equals the sum of
* all its divisors (including 1 but not including itself).
* For example 6 = 1+2+3 is perfect
28 = 14 + 7 + 4 + 2 + 1 is perfect too. ..

now...going beyond my knowledge,

How do I make the user type whatever number and then the program decides whether it's perfect or not? maybe I'm just stuck on the for-loop conditions....

right now the program searches by itself the perfect numbers from 0 to 100000.

here is the code. Any ideas?

class Perfect
{
public static boolean isPerfect( int n )
{
if (n <= 0) return false;
int sum = 0;
for (int i=1; i<n; i++)
{
if (n % i == 0)
{
sum = sum + i;
if (sum > n) return false;
}
}
return sum == n;
}

public static void main( String args[] )
{
int count = 0;
int max = 100000;
for (int n=1; n<=max; n++)
{
if (isPerfect(n))
{
System.out.println("Found perfect number: "+n);
count = count+1;
}
if (n % 10000 == 0)
{
System.out.println("Searched up to: "+n);
}
}
System.out.println("Found "+count+
" perfect numbers between 1 and "+max);
}


}
 
Old 10-02-2003, 08:32 PM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
This'll read in a valid integer from negative to positive and fail gracefully otherwise exiting the program. You can mess about and make it do something else though but 'tis an example

Code:
  import java.io.*;

  int number=0;

  BufferedReader buffRead=new BufferedReader(new InputStreamReader(System.in)); //set up reading in standard input

     try
      {
       String temp=new String(buffRead.readLine()); // read a line from standard input

       temp=temp.trim();  // Get Rid of trailing and leading whitespace if any

       number=Integer.parseInt(temp);

      }catch(IOException e)
           {
             System.out.println("Error reading data");
             System.exit(1);
            }

       catch(NumberFormatException nfe)
           {
             System.out.println("Invalid Number");
             System.exit(1);
            }
 
Old 10-05-2003, 12:36 PM   #3
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
thanks bro. my implementation here

I figured it out. I'm posting my implementation here.

Maybe somebody else might need it in the future as a reference.

randomX




public class PerfectNumber
{
public static void main(String [] args)
{
int numberEntered, iteration, add;

System.out.println();
System.out.print("Enter a positive integer: ");
numberEntered = <some method to get input from keyboard>;

while (numberEntered <= 0)
{
System.out.println();
System.out.print("Only positive integers are accepted. Please try again: ");
numberEntered = <some method to get input from keyboard>;
System.out.println();
}



add = 0; //initialization

for (iteration = 1; iteration < numberEntered; iteration++ )
{
if (numberEntered % iteration == 0 ) // check if it's even
{
add = add + iteration;
}
}
if (add == numberEntered) //compares the result to what the user typed in
{
System.out.println();
System.out.println("" + numberEntered + " is a perfect number." );
System.out.println();
}
else
{
System.out.println();
System.out.println("" + numberEntered + " is NOT a perfect number. Sorry." );
System.out.println();
}
}

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java v1.5 -- installing/finding/using hanasi Suse/Novell 5 08-12-2005 08:47 AM
edonkey java gui not finding java Sephiroth Linux - Software 0 03-13-2004 08:14 PM
floating point numbers in java spyghost Programming 8 09-06-2003 06:21 PM
finding java path? pyropenguin Linux - Newbie 2 09-19-2002 01:02 PM
finding java path? pyropenguin Linux - Software 2 09-15-2002 07:55 AM


All times are GMT -5. The time now is 10:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration