Let say N is the given number. You need to write N as summation of n numbers in m different ways without repetition.
For example N is 5 then possible results are
N = 5
- 1 1 1 1 1
- 1 1 1 2
- 1 2 2
- 1 1 3
- 1 4
- 2 3
N = 6
- 1 1 1 1 1 1
- 1 1 1 1 2
- 1 1 2 2
- 2 2 2
- 1 1 1 3
- 1 2 3
- 3 3
- 1 1 4
- 2 4
- 1 5
I have implemented by using Java
Code
public void algorithm(String result, int n, int max){
if(n==0){
System.out.println(result);
return;
}
for(int i=1; i<=max && i<=n; i++)
algorithm(i+" "+result, n-i,i);
}
For example N is 5 then possible results are
N = 5
- 1 1 1 1 1
- 1 1 1 2
- 1 2 2
- 1 1 3
- 1 4
- 2 3
N = 6
- 1 1 1 1 1 1
- 1 1 1 1 2
- 1 1 2 2
- 2 2 2
- 1 1 1 3
- 1 2 3
- 3 3
- 1 1 4
- 2 4
- 1 5
I have implemented by using Java
Code
public void algorithm(String result, int n, int max){
if(n==0){
System.out.println(result);
return;
}
for(int i=1; i<=max && i<=n; i++)
algorithm(i+" "+result, n-i,i);
}
Great Info! ...Thanks for sharing information with us.
ReplyDeleteDevelop Big Data Consulting Services
Happy to see you referring my blog.
DeletePlease follow my Github page for more programs like this
https://github.com/sathishmepco/CoreJava-Projects