-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·94 lines (84 loc) · 1.99 KB
/
Program.cs
File metadata and controls
executable file
·94 lines (84 loc) · 1.99 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections;
using System.Linq;
namespace CSC254_hw01_CS
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Please enter a number: ");
Prime_Partition (Int32.Parse(Console.ReadLine()));
foreach(ArrayList l in global_list){
foreach(int element in l){
if (l.IndexOf (element) != l.Capacity - 1 && l.Capacity != 1) {
Console.Write (element + "+");
} else {
Console.Write (element);
}
}
Console.WriteLine ("");
}
}
//Check if the number is a prime number
public static bool isPrime(int num)
{
if(num==1){
return false;
}
if(num==2){
return true;
}
int bound = (int)Math.Floor (Math.Sqrt (num));
for(int i=2; i<=bound; i++){
if(num % i == 0){
return false;
}
}
return true;
}
//Generate a list of prime numbers up to num
public static ArrayList Prime_Generator (int num)
{
ArrayList list = new ArrayList ();
for(int i=1; i<=num; i++){
if(isPrime(i)){
list.Add (i);
}
}
return list;
}
private static ArrayList global_list = new ArrayList();
//Returns a list of lists, each represents a valid prime partition
public static void Prime_Partition (int num)
{
int [] primes = Prime_Generator (num).OfType<int>().ToArray();
int length = primes.Length;
if(isPrime(num)){
ArrayList l = new ArrayList (new int[]{num});
global_list.Add (l);
}
for(int i=2; i<=length; i++){
int [] data = new int[i];
Combinations (primes, data, 0, length - 1, 0, i, num);
}
}
private static void Combinations(int[] ary, int[] data, int start, int end, int index, int r, int num)
{
if(index == r){
int sum = 0;
for(int q=0; q<r; q++){
sum += data [q];
}
if(sum==num){
global_list.Add (new ArrayList (data));
}
return;
}
for (int i=start; i<=end && end-i+1 >= r-index; i++){
data[index] = ary[i];
Combinations(ary, data, i+1, end, index+1, r, num);
}
}
}
}