Advertisement
brilliant_moves

FriendsNumbers.java

Oct 4th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.49 KB | None | 0 0
  1. import java.util.Scanner; // to read data
  2.  
  3. public class FriendsNumbers {
  4.  
  5.     /**
  6.     *   Program:    FriendsNumbers.java
  7.     *   Purpose:    Solve maths problem for Yahoo! Answers
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    04.10.2013
  10.     */
  11.  
  12. /*
  13.  The sum of all natural ( positive integer ) number of dividers 220 , except
  14. himself , is equal to
  15. 1 2 4 5 10 11 20 22 44 55 110 = 284
  16. and the sum of all natural number of dividers 284 , except himself
  17. shall be equal to
  18. 1 2 4 71 142 = 220 .
  19. Find even such a pair of numbers . "
  20. The numbers 220 and 284 numbers called friends . Generally , we
  21. To say that two positive integers are friends if each of them equal
  22. with the sum of all of the other physical dividers , including 1.
  23. Write the program in Java that solves the problem of finding all pairs
  24. Friends of numbers from 2 to N. The program accepts as input the number N and
  25. to calculate and print all your friends numbers from 2 to N.
  26. */
  27.  
  28.     public static void main (String[] args) {
  29.  
  30.         Scanner scan = new Scanner (System.in);
  31.  
  32.         int n, sum1=0, sum2=0;
  33.         System.out.print("Enter number N: ");
  34.         n = Integer.parseInt(scan.nextLine());
  35.         System.out.println();
  36.         for (int i=2; i<=n; i++) {
  37.             sum1=0;
  38.             sum2=0;
  39.             for (int j=1; j<i; j++) {
  40.                 if (i%j==0) {
  41.                     sum1+=j;
  42.                 }
  43.             }
  44.  
  45.             for (int j=1; j<sum1; j++) {
  46.                 if (sum1%j==0) {
  47.                     sum2+=j;
  48.                 }
  49.             }
  50.            
  51.             if (sum2 == i && sum1 < sum2) {
  52.                 System.out.println(sum1+" and "+sum2+" are friends numbers");
  53.             }
  54.         }
  55.     } // end main
  56.  
  57. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement