Recursion: Java programming with integer partitioning to achieve __ programming

Source: Internet
Author: User

Integer Partitioning:

Positive integer n represents the sum of a series of positive integers:

N=n1+n2+n3...+nk (of which, n1>=n2>=n3...>=nk>=1,k>=1), p (n) is the number of different partitions of positive integer n, that is, the number of positive integers.

So in all the different divisions of positive integer n, the number of partitions with the maximum addends not greater than M is recorded as Q (n,m). Then the recursive relationship is as follows:

(1) q (n,1) =1,n>=1

M=1, that is, the maximum number can only be 1, the division of only one: n=1+1+1+.....+1;

(2) Q (n,m) =q (n,n), m>=n

Since the maximum addends is greater than or equal to the positive integer n, all the maximum addends can only be n;

(3) Q (n,n) =1+q (n,n-1)

When the maximum addends is n, there is only one case. The remaining is the number of addends when the maximum is n-1;

(4) Q (n,m) =q (n,m-1) +q (n-m,m), n>m>1

When the positive integer n is greater than the maximum addends m, Q (n,m-1) represents the number of partitions when the maximum addends is less than or equal to m-1, and then the number of partitions when the maximum addends is M is added. When the maximum addends is M, the maximum addends has been fixed, and the rest is the positive integer n minus the number of integers n-m the maximum addends m, that is, Q (n-m,m).

Program:

Import Java.util.Scanner;


The public class Intdivision {

	/**
	 * Positive integer n represents the sum of a series of positive integers:
	 * N=N1+N2+N3...+NK (where n1>=n2>=n3...>=nk>= 1,k>=1)
	 * p (N) is the number of different partitions of positive integer n, that is, the dividing number of positive integers
	 */
	int intdivision (int n,int m) {
		if n<1| | M<1) return 0;
		if (n==1| | M==1) return 1;
		if (n<m) return intdivision (n, n);
		if (n==m) return intdivision (n, m-1) +1;
		Return Intdivision (n, m-1) +intdivision (N-m, M);
	}
	public static void Main (string[] args) {
		Scanner in=new Scanner (system.in);
		System.out.print ("Enter a positive integer to be Divided");
		int N=in.nextint ();
		System.out.print ("Input Max Addends");
		int M=in.nextint ();
		Intdivision id=new intdivision ();
		
		System.out.println ("integer" +n+ "" + "in the case of the maximum Addends" +m+ "is the number of divisions:" +id.intdivision (n, m));

	}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.