Partition of a positive integer N

5

1

Find the number of partitions of a positive integer n. Hope someone uses the explicit finite formula for the partition function defined in Jan Hendrik Brunier and Ken Ono paper here.

Sample Input
8
Sample Output
22

N is less than or equal to 1000. Write the complete program. More than anything I would like to see the different approaches.

fR0DDY

Posted 2011-02-10T16:38:30.360

Reputation: 4 337

Question was closed 2014-12-26T07:24:38.223

I added a winning criterion... – Timtech – 2014-12-25T22:16:29.540

but a really deus-ex-machina one (IMO) – proud haskeller – 2014-12-25T22:59:55.440

Answers

1

Java Solution

 public class IntegerPartition {
        public static void main(String[] args) {
            int n = 8; //set integer here!!
            int[][] a = new int[n + 1][n + 1];
            int i, j, k;
            for (i = 0; i < a.length; i++) {
                a[i][0] = 0;
                a[0][i] = 1;
            }
            for (i = 1; i < a.length; i++) {
                a[i][1] = 1;
                for (j = 2; j < a[0].length; j++) {
                    k = i - j;
                    if (k < 0)
                        a[i][j] = a[i][j - 1];
                    else
                        a[i][j] = a[i][j - 1] + a[k][j];
                }
            }
            i--;
            int answer = a[i][i - 1];
            System.out.println(answer + 1);
        }
    }

Aman ZeeK Verma

Posted 2011-02-10T16:38:30.360

Reputation: 609

2

Python (109 89)

n=input()
s=0
l=[(n,n)]
while l:a,i=l.pop(0);s+=a==0;l+=[(a-i,i),(a,i-1)]*(a*i>0)
print s

Hoa Long Tam

Posted 2011-02-10T16:38:30.360

Reputation: 1 902

1

ngn

Posted 2011-02-10T16:38:30.360

Reputation: 11 449

1

Mathematica 11 chars

Sorry, no need to implement the algorithm

PartitionsP  

Usage

%[8]  
22

Dr. belisarius

Posted 2011-02-10T16:38:30.360

Reputation: 5 345

2I have a book listing all partitions for numbers up to 10,000. Does that count as a 0 chars solution? :) – Eelvex – 2011-02-11T13:08:49.733

@Eelvex Just count the PDF length. I may accept that as a solution :) – Dr. belisarius – 2011-02-11T13:24:17.777

Well, it's ~9000chars in bzip2 but I feel this is not too fair. – Eelvex – 2011-02-11T14:10:49.177

@Eelvex Perhaps you may consider posting a gzipped ASCII. version. :D – Dr. belisarius – 2011-02-11T14:29:34.497

It's a fair answer. The question is not tagged code-golf, so there may be more interesting answers in other languages – gnibbler – 2011-02-22T01:45:43.243

@gnibbler sure, but it's also useful to know that http://www.wolframalpha.com/input/?i=PartitionsP+%2830%29 will return the answer, as it is based on Mathematica.

– Dr. belisarius – 2011-02-22T01:59:50.350

@belisarius, There are probably a lot of good code-challenge questions to be found by looking at the capabilities of Mathematica :) It certainly doesn't hurt to have the answers here even for languages that have it builtin (unless it is supposed to be a code-golf question). I think those are the type of things that are more likely to attract search engine traffic :) – gnibbler – 2011-02-22T04:39:19.637

1

Ruby - 95 chars

Based on Hoa Long Tam's solution

s=0
l=[[n=gets.to_i,n]]
(a,i=l.pop;a>0&&i>0&&l+=[[a-i,i],[a,i-1]];a==0&&s+=1)until l==[]
p s

Wile E. Coyote

Posted 2011-02-10T16:38:30.360

Reputation: 943

1

Perl, 92 chars

Recursive solution using p(k,n) = number of partitions of n using numbers >= k. Uses dynamic programming and partitions 1000 in 2s on my machine.

sub p{my($k,$n)=@_;$c{$k,$n}||($c{$k,$n}=$k<$n?p($k+1,$n)+p($k,$n-$k):$n==$k)}print p(1,pop)

Ungolfed:

sub p {
    my ( $k, $n ) = @_;
    $c{$k,$n}
    || (
        $c{$k,$n} =
        $k < $n
            ? p($k+1,$n) + p($k,$n-$k)
            : $n == $k
    )
}
print p( 1, pop );

user475

Posted 2011-02-10T16:38:30.360

Reputation:

1

Perl, 70 chars

Since there was no time limit:

sub p{my($k,$n)=@_;$k<$n?p($k+1,$n)+p($k,$n-$k):$n==$k}print p(1,pop);

Partitions 50 in 2.3 s.

user475

Posted 2011-02-10T16:38:30.360

Reputation:

somebody downvoted everyone here so I suspect (s)he had no real reason. – Eelvex – 2011-02-15T09:40:59.857

@Eelvex yeap, flagging for mod attention – Dr. belisarius – 2011-02-15T17:04:20.577

1

J, 72

Modified from the manual:

f=:[:(0<@-."1~>@{:)](,([:<@;(-i.)@#([,.(>:{."1)#])&.>]))@]^:[(<i.1 0)"_

eg

#f 8
22

Eelvex

Posted 2011-02-10T16:38:30.360

Reputation: 5 204

0

Haskell, 51 39

f=(%1)
0%k=1
n%k=sum[(n-r)%r|r<-[k..n]]

the % function gets two parameters - n and k, and returns the amount of partitions of n at which every part is at least k.

%'s definition works by choosing r to be the next part of the partition. we compute the amount of possibilities when this is the next part (note that all consequent parts must be bigger or equal to it) by a recursive call and sum all possibilities.

proud haskeller

Posted 2011-02-10T16:38:30.360

Reputation: 5 866