Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself

«
»
This is a java program to find the number of ways to write a given number as sum of numbers less than the number itself. We start with the number, number minus one is the next partition and so on, till all one’s are the last partition where we stop.

Here is the source code of the Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the number of ways to write a number as a sum of smaller than the number itself
  2. import java.util.Scanner;
  3.  
  4. public class NumberOf_Unique_Partitions 
  5. {
  6.     public static void print(int[] p, int n, int count) 
  7.     {
  8.         for (int i = 0; i < n; i++)
  9.             System.out.print(p[i] + " ");
  10.         System.out.println();
  11.         int j;
  12.         for (j = 0; j < n; j++) 
  13.         {
  14.             if (p[j] == 1)
  15.                 continue;
  16.             else
  17.                 break;
  18.         }
  19.         if (j == n)
  20.             System.out
  21.                     .println("The number of ways to write a number as a sum of number smaller than itself is :"
  22.                             + (count - 1));
  23.     }
  24.  
  25.     public static void generateUniquePartition(int n) 
  26.     {
  27.         int[] p = new int[n];
  28.         int k = 0, count = 0;
  29.         p[k] = n;
  30.         while (true) 
  31.         {
  32.             count++;
  33.             print(p, k + 1, count);
  34.             int rem_value = 0;
  35.             while (k >= 0 && p[k] == 1) 
  36.             {
  37.                 rem_value += p[k];
  38.                 k--;
  39.             }
  40.             if (k < 0)
  41.                 return;
  42.  
  43.             p[k]--;
  44.             rem_value++;
  45.  
  46.             while (rem_value > p[k]) 
  47.             {
  48.                 p[k + 1] = p[k];
  49.                 rem_value -= p[k];
  50.                 k++;
  51.             }
  52.             p[k + 1] = rem_value;
  53.             k++;
  54.         }
  55.     }
  56.  
  57.     public static void main(String args[]) 
  58.     {
  59.         System.out.println("Unique Partitioning of a given number");
  60.         System.out.println("Enter the number:");
  61.         Scanner sc = new Scanner(System.in);
  62.         int n = sc.nextInt();
  63.         generateUniquePartition(n);
  64.         sc.close();
  65.     }
  66. }

Output:

advertisement
$ javac NumberOf_Unique_Partitions.java
$ java NumberOf_Unique_Partitions
 
Unique Partitioning of a given number
Enter the number:
6
6 
5 1 
4 2 
4 1 1 
3 3 
3 2 1 
3 1 1 1 
2 2 2 
2 2 1 1 
2 1 1 1 1 
1 1 1 1 1 1 
The number of ways to write as a sum of number smaller than itself is :10

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter