code_junkie

Prime numbers

Nov 14th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. int main() {
  8. ifstream trial;
  9. trial.open("C:\Users\User\Documents\trial.txt");
  10. int prime;
  11. trial >> prime;
  12. ofstream write;
  13. write.open("C:\Users\User\Documents\answer.txt");
  14. int num[100], b, c, e;
  15. bool check;
  16. b = 0;
  17. switch (prime) {
  18. case 1:
  19. {
  20. write << 2 << endl;
  21. break;
  22. }
  23. case 2:
  24. {
  25. write << 3 << endl;
  26. break;
  27. }
  28. case 3:
  29. {
  30. write << 5 << endl;
  31. break;
  32. }
  33. case 4:
  34. {
  35. write << 7 << endl;
  36. break;
  37. }
  38. default:
  39. {
  40. for (int a = 10; a <= 1000000; a++) {
  41. check = false;
  42. if (((a % 2) != 0) && ((a % 3) != 0) && ((a % 5) != 0) && ((a % 7) != 0)) // first filter
  43. {
  44. for (int d = 0; d <= b; d++) {
  45. c = num[d];
  46. if ((a % c) == 0) {
  47. check = true; // second filter based on previous recorded primes in array
  48. break;
  49. }
  50.  
  51. }
  52. if (!check) {
  53. e = a;
  54. if (b <= 100) {
  55. num[b] = a;
  56. }
  57.  
  58. b = b + 1;
  59. }
  60. }
  61. if ((b) == (prime - 4)) {
  62. write << e << endl;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. trial.close();
  69. write.close();
  70. return 0;
  71. }
  72.  
  73. for( int d=0; d<=b; d++)
  74.  
  75. for( int d=0; d<b; d++)
  76.  
  77. int someArray[100];
  78. someArray[150] = 10;
  79.  
  80. #include <vector> // std::vector
  81.  
  82. ...
  83.  
  84. const int MAX_ITEMS = 100;
  85.  
  86. std::vector<int> intVector;
  87.  
  88. intVector.reserve(MAX_ITEMS); // allocates all memory up-front
  89.  
  90. // add items
  91. for (int i = 0; i < MAX_ITEMS; i++)
  92. {
  93. intVector.push_back(i); // this is how you add a value to a vector;
  94. }
  95.  
  96. // print them
  97. for (int i = 0; i < intVector.size(); i++)
  98. {
  99. int elem = intVector[i]; // this access the item at index 'i'
  100. printf("element %d is %dn", i, elem);
  101. }
  102.  
  103. #include <iostream>
  104. #include <list>
  105. #include <math.h>
  106. #include <functional>
  107. #include <algorithm>
  108.  
  109. using namespace std;
  110.  
  111. class is_multiple : public binary_function<int, int, bool>
  112. {
  113. public:
  114. bool operator()(int value, int test) const
  115. {
  116. if(value == test) // do not remove the first value
  117. return false;
  118. else
  119. return (value % test) == 0;
  120. }
  121. };
  122.  
  123. int main()
  124. {
  125. list<int> numbersToTest;
  126. int input = 500;
  127.  
  128. // add all numbers to list
  129. for(int x = 1; x < input; x++)
  130. numbersToTest.push_back(x);
  131.  
  132. // starting at 2 go through the list and remove all multiples until you reach the squareroot
  133. // of the last element in the list
  134. for(list<int>::iterator itr = ++numbersToTest.begin(); *itr < sqrt((float) input); itr++)
  135. {
  136. int tmp = *itr;
  137. numbersToTest.remove_if(bind2nd(is_multiple(), *itr));
  138. itr = find(numbersToTest.begin(), numbersToTest.end(), tmp); //remove_if invalidates iterator
  139. // so find it again. kind of ugly
  140. }
  141.  
  142. // output primes
  143. for(list<int>::iterator itr = numbersToTest.begin(); itr != --numbersToTest.end(); itr++)
  144. cout << *itr << "t";
  145.  
  146. system("PAUSE");
  147.  
  148. return 0;
  149. }
  150.  
  151. for (int d = 0; d <= b; d++) {
  152. c = num[d];
  153.  
  154. for(int currentInt=2; currentInt<=1000000; currentInt++)
  155.  
  156. {check = false; // Basically the idea for this for loop is to run checks against integers. This is the main for loop in this program. I re initialize check to false ( check is a bool declared above this. )
  157.  
  158. for( int arrayPrime=0; arrayPrime<currentPrime; arrayPrime++) // This for loop is used for checking the currentInt against previously found primes which are stored in the num array.
  159.  
  160. { c=num[arrayPrime];
  161. if ((currentInt%c)==0) { check = true;// second filter based on previous recorded primes in array
  162. break;} // this is the check. I check the number against every stored value in the num array. If it's divisible by any of them, then bool check is set to true.
  163.  
  164.  
  165. if ( currentInt == 2)
  166. { check = false; } // since i preset num[0] = 2 i make an exception for the number 2.
  167.  
  168. if (!check)
  169. {
  170. e=a;
  171. if(currentPrime <= 100){
  172. num[currentPrime]= currentInt;} // This if uses check to see if the currentInt is a prime.
  173.  
  174. currentPrime = currentPrime+1;} // increases the value of currentPrime ( previously b ) by one if !check.
  175.  
  176. if(currentPrime==prime)
  177. {
  178. write<<e<<endl;
  179. break;} // if currentPrime == prime then write the currentInt into a txt file and break loop, ending the program.
  180.  
  181. #include <cstdio>
  182. #include <iostream>
  183. #include <cstdlib>
  184. #include <fstream>
  185.  
  186. using namespace std;
  187.  
  188. int main()
  189. {
  190. ifstream trial;
  191. trial.open("C:\Users\User\Documents\trial.txt");
  192. int prime, e;
  193. trial>>prime;
  194. ofstream write;
  195. write.open("C:\Users\User\Documents\answer.txt");
  196. int num[10000], currentPrime, c, primePrint;
  197. bool check;
  198. currentPrime=0;
  199. num[currentPrime] = 2;
  200. currentPrime=1;
  201.  
  202. for(int currentInt=2; currentInt<=1000000; currentInt++)
  203. {check = false;
  204. for( int arrayPrime=0; arrayPrime<currentPrime; arrayPrime++)
  205. { c=num[arrayPrime];
  206. if ((currentInt%c)==0) { check = true;// second filter based on previous recorded primes in array
  207. break;}
  208. }
  209.  
  210.  
  211. if (!check)
  212. { e=currentInt;
  213. if( currentInt!= 2 ) {
  214. num[currentPrime]= currentInt;}
  215. currentPrime = currentPrime+1;}
  216. if(currentPrime==prime)
  217. {
  218. write<<e<<endl;
  219. break;}
  220. }
  221. trial.close();
  222. write.close();
  223. return 0;
  224. }
  225.  
  226. #include <iostream>
  227. #include <fstream>
  228. #include <cmath>
  229. using namespace std;
  230.  
  231. int main()
  232. {
  233. int m;
  234. int n=0;
  235. char ch;
  236. fstream fp;
  237. cout<<"What prime numbers do you want get within? ";
  238. if((cin>>m)==0)
  239. {
  240. cout<<"Bad input! Please try again!n";
  241. return 1;
  242. }
  243. if(m<2)
  244. {
  245. cout<<"There are no prime numbers within "<<m<<endl;
  246. return 0;
  247. }
  248. else if(m==2)
  249. {
  250. fp.open("prime.txt",ios::in|ios::out|ios::trunc);//create a file can be writen and read. If the file exist, it will be overwriten.
  251. fp<<"There are only 1 prime number within 2.n";
  252. fp<<"2n";
  253. fp.close();
  254. cout<<"Congratulations! It has worked out!n";
  255. return 0;
  256. }
  257. else
  258. {
  259. int j;
  260. int sq;
  261. fp.open("prime.txt",ios::in|ios::out|ios::trunc);
  262. fp<<"2tt";
  263. n++;
  264. for(int i=3;i<=m;i+=2)
  265. {
  266. sq=static_cast<int>(sqrt(i))+1;
  267. fp.seekg(0,ios::beg);
  268. fp>>j;
  269. for(;j<sq;)
  270. {
  271. if(i%j==0)
  272. {
  273. break;
  274. }
  275.  
  276. else
  277. {
  278. if((fp>>j)==NULL)
  279. {
  280. j=3;
  281. }
  282. }
  283. }
  284. if(j>=sq)
  285. {
  286. fp.seekg(0,ios::end);
  287. fp<<i<<"tt";
  288. n++;
  289. if(n%4==0)
  290. fp<<'n';
  291. }
  292. }
  293. fp.seekg(0,ios::end);
  294. fp<<"nThere are "<<n<<" prime number within "<<m<<".n";
  295. fp.close();
  296. cout<<"Congratulations! It has worked out!n";
  297. return 0;
  298. }
  299. }
Add Comment
Please, Sign In to add comment