Find politeness of a number in java

Java Programming Java.Util PackageJava8

The numbers which can be expressed as the sum of positive consecutive integers are known as polite numbers.

Ex: 5 = 2+3

The number of ways a number can be expressed as the sum of positive integers will be the Politeness of that number.

Ex: 9 = 4+5 || 2+3+4

Algorithm

  • Get the prime factors of a number.
  • Get the powers of prime factors greater than 2.
  • Add 1 to all of them.
  • Multiply them, subtract 1 from the result.

Program

import java.util.Scanner;

public class PolitenessOfANumber {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a number");
      int num = sc.nextInt();
      int count = 0, result = 1;
     
      for(int i = 2; i< num; i++) {
         while(num%i == 0) {
            System.out.println(i+" ");
            num = num/i;
            if(i>2) {
               count ++;
            }
            result = result*(count+1);
         }
         if(num >2) {
            System.out.println(num);
         }
         System.out.println("Politeness of the given number is : "+(result-1));
      }
   }
}

Output

Enter a number
216
2
2
2
3
3
3
Politeness of the given number is : 3
raja
Updated on 25-Jun-2020 12:17:45

Advertisements