This is a java program to perform partition of an integer in all possible ways. Every partition when added should result in the given integer.
Here is the source code of the Java Program to Perform Partition of an Integer in All Possible Ways. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is sample program to print a unique partitions of a given number import java.util.Scanner; public class Unique_Partitions_Number { public static void print(int[]p, int n) { for(int i=0; i<n; i++) System.out.print(p[i]+" "); System.out.println(); } public static void generateUniquePartition(int n) { int []p = new int[n+n]; int k = 0; p[k] = n; while(true) { print(p, k=1); int rem_value = 0; while(k >= 0 && p[k] == 1) { rem_value += p[k]; k--; } if(k < 0) return; p[k]--; rem_value++; while(rem_value > p[k]) { p[k+1] = p[k]; rem_value -= p[k]; k++; } p[k+1] = rem_value; k++; } } public static void main(String args[]) { System.out.println("Unique Partitioning of a given number"); System.out.println("Enter the number:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); generateUniquePartition(n); sc.close(); } }
Output:
$ javac Unique_Partitions_Number.java $ java Unique_Partitions_Number Unique Partitioning of a given number Enter the number: 4 4 3 1 2 2 2 1 1 1 1 1 1
Related posts:
Working with Kotlin and JPA
Returning Custom Status Codes from Spring Controllers
A Guide to Spring Cloud Netflix – Hystrix
Remove All Occurrences of a Specific Value from a List
JWT – Token-based Authentication trong Jersey 2.x
Java Program to Implement Bresenham Line Algorithm
Guide to Java Instrumentation
Static Content in Spring WebFlux
Control the Session with Spring Security
Spring REST API with Protocol Buffers
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Implement ArrayDeque API
Lập trình mạng với java
Java 8 and Infinite Streams
Chuyển đổi Array sang ArrayList và ngược lại
Java InputStream to Byte Array and ByteBuffer
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Hướng dẫn Java Design Pattern – Interpreter
The Difference Between map() and flatMap()
Java Program to Implement Fibonacci Heap
Write/Read cookies using HTTP and Read a file from the internet
Remove HTML tags from a file to extract only the TEXT
Converting String to Stream of chars
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement Find all Back Edges in a Graph
Comparing Two HashMaps in Java
Spring Web Annotations
Spring Data JPA Delete and Relationships
Hướng dẫn Java Design Pattern – Observer
Tìm hiểu về Web Service
Login For a Spring Web App – Error Handling and Localization
Hướng dẫn Java Design Pattern – Abstract Factory