Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // to read data
- public class FriendsNumbers {
- /**
- * Program: FriendsNumbers.java
- * Purpose: Solve maths problem for Yahoo! Answers
- * Creator: Chris Clarke
- * Created: 04.10.2013
- */
- /*
- The sum of all natural ( positive integer ) number of dividers 220 , except
- himself , is equal to
- 1 2 4 5 10 11 20 22 44 55 110 = 284
- and the sum of all natural number of dividers 284 , except himself
- shall be equal to
- 1 2 4 71 142 = 220 .
- Find even such a pair of numbers . "
- The numbers 220 and 284 numbers called friends . Generally , we
- To say that two positive integers are friends if each of them equal
- with the sum of all of the other physical dividers , including 1.
- Write the program in Java that solves the problem of finding all pairs
- Friends of numbers from 2 to N. The program accepts as input the number N and
- to calculate and print all your friends numbers from 2 to N.
- */
- public static void main (String[] args) {
- Scanner scan = new Scanner (System.in);
- int n, sum1=0, sum2=0;
- System.out.print("Enter number N: ");
- n = Integer.parseInt(scan.nextLine());
- System.out.println();
- for (int i=2; i<=n; i++) {
- sum1=0;
- sum2=0;
- for (int j=1; j<i; j++) {
- if (i%j==0) {
- sum1+=j;
- }
- }
- for (int j=1; j<sum1; j++) {
- if (sum1%j==0) {
- sum2+=j;
- }
- }
- if (sum2 == i && sum1 < sum2) {
- System.out.println(sum1+" and "+sum2+" are friends numbers");
- }
- }
- } // end main
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement