Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008, 08:10 AM
Member
 
Join Date: Jan 2008
Posts: 26
jvasilj1 is on a distinguished road
students needs help
i have created java code to give me perfect numbers, and it works...but now im hung up on modifying the main to give me AMICABLE NUMBERS. Any ideas???????
HERE IS MY PERFECT.JAVA CODE
----------------------------------------------
// Find all perfect numbers less than
// or equal to then entered upper bound.

import javax.swing.JOptionPane;

public class Perfect
{
public static void main(String[] args)
{
int upperBound, n, factor, sum;

upperBound = Integer.parseInt(JOptionPane.showInputDialog(
"Enter upper bound for perfect number search"));

for(n = 2; n <= upperBound; n++)
{
sum = 0;
for(factor = 1; factor <= n / 2; factor++)
if (n % factor == 0)
sum += factor;

if (n == sum)
System.out.println(n);
}
}
}

// Prompt of input dialog:
Upper bound for perfect number search:

// Sample input to input dialog:
10000

// Output in Terminal Window:
6
28
496
8128
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-30-2008, 08:16 AM
Member
 
Join Date: Jan 2008
Posts: 26
jvasilj1 is on a distinguished road
THESE ARE THE INSTRUCTIONS
------------------------------------------------------------------------To check if n belongs to an amicable pair, find the sum (sum1) of the factors of n. Then find the sum (sum2) of all the factors of sum1. If sum2 == n, you have found a pair of amicable numbers.

Do not print any pair of perfect numbers in your output.

Do not print a pair of amicable numbers more than once.

Here are some hints for writing the source code for this project:
Write a static method (below your main method) defined like this:

public static int sumFactors(int n)
{
... // Body of method goes here.
}

It should compute the sum of all the factors of the input n.

Test your sumFactors method for finding perfect numbers before you try to use it for finding amicable numbers.

In your main method, write a double for loop that checks all the numbers n and m between 2 and upperBound. Check that the sum of the factors of n equals m and that the sum of the factors of m equals n.
-------------------------------------------------------------------
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-30-2008, 09:30 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Solution
Hello jvasilj1

Use this class. It will solve your problem.
Code:
import java.util.*; public class AmicablePairs{ protected static Vector<Integer> getFactors(int number){ Vector<Integer> result = new Vector<Integer>(); for (int i = 1 ; i < number; i++){ if (number % i == 0) result.add(new Integer(i)); } return result; } protected static int sumNumbers(Vector<Integer> numbers){ int result = 0; for (Integer integer : numbers) result += integer; return result; } protected static void printNumbers(Vector<Integer> numbers){ for (Integer integer : numbers) System.out.println(integer); System.out.println(); } public AmicablePairs(int bound){ for (int m = 2; m <= bound; m++){ for (int n = 2; n <= bound; n++){ if ( (sumNumbers(getFactors(m)) == n) && (sumNumbers(getFactors(n)) == m)) { System.out.println(m + ", " + n); } } } } }
Just use the constructor to run it:
Code:
new AmicablePairs(1000);
That should help you. Ask, if you need help to understand this class.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 04:52 AM.


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