SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.Threading; | |
4 | ||
5 | class SieveOfEratosthenes | |
6 | { | |
7 | //Write a program that finds all prime numbers in the range [1...10 000 000]. Use the sieve of Eratosthenes algorithm (find it in Wikipedia). | |
8 | ||
9 | static void Main() | |
10 | { | |
11 | int endInterval = 10000000; | |
12 | bool[] sieve = new bool[endInterval]; | |
13 | ||
14 | sieve[0] = true; | |
15 | sieve[1] = true; | |
16 | ||
17 | int lastCheck = (int)Math.Sqrt(sieve.Length); | |
18 | ||
19 | for (int index = 2; index <= lastCheck; index++) | |
20 | { | |
21 | if (!sieve[index]) | |
22 | { | |
23 | for (int j = index * index; j < sieve.Length; j += index) | |
24 | { | |
25 | sieve[j] = true; | |
26 | } | |
27 | } | |
28 | } | |
29 | ||
30 | List<int> primesList = new List<int>(); | |
31 | ||
32 | for (int index = 2; index < sieve.Length; index++) | |
33 | { | |
34 | if (!sieve[index]) | |
35 | { | |
36 | primesList.Add(index); | |
37 | } | |
38 | } | |
39 | ||
40 | Console.WriteLine(primesList.Count);//test: http://primes.utm.edu/howmany.shtml | |
41 | RithmOfPrimes(primesList); | |
42 | - | RithmOfPrimes(primesList); |
42 | + | |
43 | ||
44 | private static void RithmOfPrimes(List<int> primesList) | |
45 | { | |
46 | int frequency = 440; | |
47 | int duration = 170; | |
48 | ||
49 | - | int counter = primesList.Count; |
49 | + | |
50 | { | |
51 | Console.Clear(); | |
52 | Thread.Sleep(duration + 1); //allows to hear full sound duration | |
53 | Console.WriteLine(prime); | |
54 | ||
55 | if (HasEvenDigitInLast2Digits(prime, ref frequency)) | |
56 | { | |
57 | - | if (HasEvenDigitInLast2Digits(prime)) |
57 | + | Console.Beep(frequency, duration << 1); |
58 | } | |
59 | else | |
60 | { | |
61 | Console.Beep(frequency, duration); | |
62 | } | |
63 | - | Console.Beep(frequency<<1, duration); |
63 | + | |
64 | } | |
65 | ||
66 | private static bool HasEvenDigitInLast2Digits(int prime, ref int frequency) | |
67 | { | |
68 | - | private static bool HasEvenDigitInLast2Digits(int prime) |
68 | + | |
69 | int len = 2; | |
70 | ||
71 | while (count++ < len) | |
72 | - | while (count++ < 2) |
72 | + | |
73 | int digit = prime % 10; | |
74 | ||
75 | if ((digit & 1) == 0 && digit != 0) | |
76 | { | |
77 | frequency = SetFrequency(digit); | |
78 | return true; | |
79 | } | |
80 | ||
81 | frequency = SetFrequency(digit); | |
82 | prime /= 10; | |
83 | } | |
84 | ||
85 | return false; | |
86 | } | |
87 | ||
88 | private static int SetFrequency(int digit) | |
89 | { | |
90 | int frequency = 0; | |
91 | ||
92 | switch (digit) //http://www.phy.mtu.edu/~suits/notefreqs.html | |
93 | { | |
94 | // sqrt is integer | |
95 | case 1: | |
96 | case 4: | |
97 | case 9: frequency = 392/*g4*/; break; | |
98 | ||
99 | //prime | |
100 | case 2: | |
101 | case 3: | |
102 | case 5: | |
103 | case 7: frequency = 494/*b4*/; break; | |
104 | ||
105 | //composite | |
106 | case 6: | |
107 | case 8: frequency = 440/*a4*/; break; | |
108 | ||
109 | //zero | |
110 | case 0: frequency = 294/*d4*/; break; | |
111 | } | |
112 | return frequency; | |
113 | } | |
114 | } |