.. ..
()
1
:
.. -212
..
:
. .
2012.
1. .
1. :
1. SinInt.cpp - sin(x) 0 Pi n (1 .).
2. PrimeCnt.cpp - 0..n (100 .).
2. .
2. C++.
// SinInt2.cpp: .
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <math.h>
using namespace std;
void Linear_Calculation(void); //
void Parallel_Computation(void); // ()
double Sum_Element(int); // ( )
// integral[a, b](sin(x)dx) = sum([a+b]/n * sin[bi])
const double a = 0;
const double b = atan((double)1)*4;
const long int n = 1000000000;
int Number_ThReads = 1; //
double Resultat = 0; //
DWORD Time_CPU; //
int main()
{
setlocale(LC_ALL, "RUS");
unsigned Num_menu;
cout << " (sin(x)dx) 0 \n\n";
cout << "\n \n";
cout << "1. \n";
cout << "2. \n";
cout << "3. \n";
cout << "\n : ";
cin >> Num_menu;
switch(Num_menu) {
case 1: {
Linear_Calculation();
break;
}
case 2: {
Parallel_Computation();
break;
}
default: return 1;
}
system("pause");
return 0;
}
double Sum_Element(int The_number_of_threads) // ( )
{
long int number_passes = 0;
double Sum_sin= 0;
double h = (a+b)/n;
double i = The_number_of_threads + 1;
while(i<=n)
{
Sum_sin += sin(i * h) * h;
i += Number_ThReads;
number_passes++;
}
cout << "\n = " << fixed << number_passes << " " << The_number_of_threads;
//fixed .
return Sum_sin;
}
|
|
void Linear_Calculation() //
{
cout << "\n , ....\n";
Time_CPU= GetTickCount(); //
cout.precision(16); // .
Number_ThReads = 1; // 1
cout << "\n\n : " << (double)Sum_Element(0) << endl;
cout.precision(2); // .
Time_CPU = (GetTickCount() - Time_CPU) / 1000; // (/ 1000-
cout << "\n : " << fixed << (float)Time_CPU << "" << "\n" << endl;
}
DWORD WINAPI ThRead_Function(LPVOID lpvThParam) //
{
Resultat += Sum_Element((int)lpvThParam);
return 0;
}
void Parallel_Computation()// ()
{
cout << " : ";
cin >> Number_ThReads;
cout << "\n , ....\n";
HANDLE *a_pointer_to_stream = new HANDLE[Number_ThReads]; //
DWORD Pointer_ThRead_Identifier; //
Time_CPU=GetTickCount(); //
for(int t_id = 0; t_id < Number_ThReads; t_id++) //
{
a_pointer_to_stream[t_id] = CreateThread(NULL, 0, ThRead_Function, (LPVOID)t_id, 0, &Pointer_ThRead_Identifier);
//
}
for(int t_id = 0; t_id < Number_ThReads; t_id++)
{
WaitForSingleObject(a_pointer_to_stream[t_id], INFINITE); //
// hHandle (a_pointer_to_stream[t_id]),
// (INFINITE - )
CloseHandle(a_pointer_to_stream[t_id]); // a_pointer_to_stream[tid]
}
cout.precision(16); // .
cout << "\n\n : " << double(Resultat) << endl;
cout << fixed; //fixed .
cout.precision(2); // .
Time_CPU = (GetTickCount() - Time_CPU) / 1000; // (/ 1000-
cout << "\n : " << fixed << (float)Time_CPU << "" << "\n"<< endl;
delete[]a_pointer_to_stream; //
}
3. #include "stdafx.h"
4.
5. int n=10000000; //
6. int kolProst=1;
7. double t1, t2;
8.
9. struct Data{
10. int min;
11. int max;
12. };
13.
14. DWORD WINAPI Poisk_prost(LPVOID lpvThParam){
15. Data *par=(Data*)lpvThParam;
16. int min=par->min;
17. int max=par->max;
18. int kol=0;
19. if (min%2==0) min++;
20. if (max%2==0) max--;
21. for (int i=min; i<=max; i+=2){
22. if (i==3) kol++;
23. for (int j = 3; j<i; j+=2){
|
|
24. if (i%j==0) {
25. break;
26. }
27. if (j*j-1>i) {
28. kol++;
29. break;
30. }
31. }
32. }
33. kolProst+=kol;
34. return 0;
35. }
36.
37. int main()
38. {
39. t1 = (double)clock();
40. setlocale(LC_ALL, "RUS");
41. cout << " ";
42. freopen("INPUT.TXT", "r", stdin);
43. freopen("OUTPUT.TXT", "w", stdout);
44. int kolPot=0;
45. cin >> kolPot;
46.
47. DWORD dwIdTh;
48. HANDLE *nt = new HANDLE[kolPot];
49.
50. Data str, *param;
51.
52. for(int i=1; i<=kolPot; i++){
53. str.min=(float)n/kolPot*(i-1);
54. str.max=(float)n/kolPot*i;
55. if ((str.min==0)&&(str.max>3)) {
56. str.min=3;
57. }
58.
59. if (str.max%2) str.max--;
60. param=&str;
61. nt[i-1]= CreateThread(NULL, 0, Poisk_prost, (LPVOID)param, 0, &dwIdTh);
62. WaitForSingleObject(nt[i-1], INFINITE);
63. }
64.
65. for(int i=0; i<kolPot; i++) CloseHandle(nt[i]);
66.
67. delete[] nt;
68. cout << "\n = " << kolPot;
69. cout.precision(16);
70. cout << "\n = " << kolProst;
71. t2 = (double)clock();
72. std::cout << "\n = " << (t2-t1);
73. return 0;
74. }
.
(sin(x)dx) 0
1.
2.
3.
: 2
: 1
, ....
= 1000000000 0
: 1.9999999999966300
: 59.00
...
(sin(x)dx) 0
1.
2.
3.
: 2
: 2
, ....
= 500000000 1
= 500000000 0
: 2.0000000000008273
: 30.00
...
(sin(x)dx) 0
1.
2.
3.
: 2
: 3
, ....
= 333333334 0
= 333333333 2
= 333333333 1
: 2.0000000000004676
: 26.00
...
(sin(x)dx) 0
1.
2.
3.
: 2
: 14
, ....
= 71428572 5
= 71428571 7
= 71428572 2
= 71428572 1
= 71428571 9
= 71428571 13
= 71428571 11
= 71428572 3
= 71428572 0
= 71428572 4
= 71428571 12
= 71428571 6
|
|
= 71428571 10
= 71428571 8
: 1.9999999999998799
: 27.00
...
= 1
= 5761455
= 700206
= 2
= 5761455
= 687755
= 3
= 5761455
= 654174
= 4
= 5761455
= 664587
= 5
= 5761455
= 660037
= 10
= 5761455
= 661512
= 15
= 5761455
= 651544
= 20
= 5761455
= 652743
= 30
= 5761455
= 655568
= 50
= 5761455
= 665444
0 pi
N / | , | |
100 5761455
N / | , | |
Windows 7 32-. , 2,5 , 3.01
. 1 20 .. , 20 (.. )
, .