Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 07:40 AM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Lite3864 is on a distinguished road
Default Efficient Perfect Number
I have code written to discover perfect numbers but my instructor wants me to make it work more efficiently. I understand what needs to happen just not how to make it happen. To make it faster I need to use Math.sqrt(). The only problem is that when doing it this way I am not saving the multiple of the lower numbers. For example 28 is a perfect number. 1+2+4+7+14=28 the problem is that by using the Math.sqrt I limit the search to everything below 5.29 which I rounded up to 6. By doing this I am still missing out on 7 and 14 which are the reciprocals to 2 and 4. Is there something I can do to save these reciprocals and only go up to the square root?

Here is what I have now:

public class Perfect //MUST match the file name!
{
public static void main (String[ ] args)
{
Scanner keyboard = new Scanner(System.in);

int limit;

System.out.println("Please enter a limit for finding perfect numbers:");
limit = keyboard.nextInt();

for (int i=1; i<= Math.ceil(Math.sqrt(limit)); i++) //By just setting
//this to limit it works just slowly.
{
int sum=0;
for (int j=1; j<i; j++)
{
if (i%j==0) sum+=j;
}
if (sum==i) System.out.println(i);
}


}//end main method
}//end class
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 08:23 AM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Lite3864 is on a distinguished road
Default
I am getting closer now I just need to figure out how to get rid of the 1 and get the 6 back. 1 is not a perfect number but 6 is. Here is what I have now.

public class Perfect
{
public static void main (String[ ] args)
{


Scanner keyboard = new Scanner(System.in);

int limit;

System.out.println("Please enter a limit for finding perfect numbers:");
limit = keyboard.nextInt();

for (int i=1; i<= limit; i++)
{
int sum=0;
for (int j=1; j<=Math.ceil(Math.sqrt(i)); j++)
{
if (i%j==0)
{
sum+=j;
//System.out.println(i+"i"+j+"j"+i/j+"t"+sum);
if ((i/j)>Math.floor(Math.sqrt(i))&& (i/j)<i)
sum+=(i/j);
}
}
if (sum==i) System.out.println(i);
}


}//end main method
}//end class
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 11:24 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Arrow lift loop invariants
Code:
class Demo
{
    int index=0;
    int sum=0;
    int limit Math.ceil(Math.sqrt(i));
    for(index; index < limit; index++)
    {
        .... code goes here ....;//move loop control variables as shown.
    }
}
continue work. A for loop is just sugar coated sytax for int limit = setLimit(); then do code while index not exceed some limit value.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Last edited by Nicholas Jordan; 11-22-2008 at 11:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2008, 07:00 PM
Member
 
Join Date: Nov 2008
Posts: 3
Rep Power: 0
Lite3864 is on a distinguished road
Default
I don't think I am following you on this one. If I make the suggested change then what will i reference to? In my code it was connected to the control loop. I guess I am confused as to which for loop you are changing.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-23-2008, 01:07 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default
move Math.ceil(Math.sqrt(i)); out of whichever loop it is a limit for.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
More efficient in memory? Multiple Jars or Single Jars with lot's of Classes dark_cybernetics New To Java 0 08-19-2008 04:44 PM
How to write efficient maintainable code. Zosden Advanced Java 9 05-01-2008 04:48 AM
Way to Java Perfect Javaisinmyblood New To Java 1 02-06-2008 11:28 PM
Perfect Squares divyachaparala New To Java 4 02-05-2008 09:21 AM
Programming in an efficient way JavaForums Java Blogs 0 11-11-2007 11:42 PM


All times are GMT +2. The time now is 03:45 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org