-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathp78.adb
50 lines (42 loc) · 1.19 KB
/
p78.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
with Ada.Containers.Indefinite_Vectors;
with Ada.Text_IO;
use Ada.Text_IO;
procedure Main is
package Integer_Vectors is new Ada.Containers.Indefinite_Vectors (Index_Type => Natural, Element_Type => Integer);
use Integer_Vectors;
P : Vector;
N : Integer := 1;
Pn : Integer;
-- Compute partition function using recurrence relation
function Partition (N : Integer) return Integer is
K : Integer := 1;
Sign : Integer := 1;
Result : Integer := 0;
Index : Integer;
begin
loop
Index := N - (K * (3 * K - 1) / 2);
if Index < 0 then
return Result;
end if;
Result := Result + Sign * P.Element (Index);
Index := N - (K * (3 * K + 1) / 2);
if Index < 0 then
return Result;
end if;
Result := Result + Sign * P.Element (Index);
K := K + 1;
Sign := -1 * Sign;
end loop;
end Partition;
begin
P.Append (1);
loop
-- Note: Since we're only adding, we can throw out higher digits
Pn := Partition (N) rem 1000000;
P.Append (Pn);
exit when Pn = 0;
N := N + 1;
end loop;
Put_Line (Integer'Image (N));
end Main;