forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_factor_sieve.cs
64 lines (52 loc) · 1.71 KB
/
prime_factor_sieve.cs
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
/* Prime Factorization using Sieve */
using System;
using System.Collections;
class PFS
{
static int MAX = 100001;
static int[] small_pf = new int[MAX];
static void prime_sieve()
{
small_pf[1] = 1;
for (int i = 2; i < MAX; i++)
small_pf[i] = i;
for (int i = 4; i < MAX; i += 2)
small_pf[i] = 2;
for (int i = 3; i * i < MAX; i++)
{
if (small_pf[i] == i)
{
for (int j = i * i; j < MAX; j += i)
if (small_pf[j] == j)
small_pf[j] = i;
}
}
}
static ArrayList get_Factor(int x)
{
ArrayList r = new ArrayList();
while (x != 1)
{
r.Add(small_pf[x]);
x = x / small_pf[x];
}
return r;
}
public static void Main()
{
prime_sieve();
Console.WriteLine("Enter a value:");
int val = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Prime factors are:");
ArrayList p = get_Factor(val);
for (int i = 0; i < p.Count; i++)
Console.Write(p[i] + " ");
Console.WriteLine("");
}
}
/* OUTPUT
Enter a value:
562
Prime factors are:
2 281
*/