473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

loop for finding possible odd divisors

I'm making a prime finder another thing I'd like is a loop to check all possible odd factors if I can do that i can find all primes under 18 quintillion I think and store them in files ( yes I have nearly 200 GB of space left). also thanks for the thread on finding position in a file it helped me stop the file getting to big too open in MS-DOS

Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #pragma hdrstop
  5.  
  6. #include <tchar.h>
  7. //---------------------------------------------------------------------------
  8.  
  9. #pragma argsused
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12. unsigned long long int i;
  13. int fs;
  14. int prime=0;
  15.     FILE *fp;
  16.     size_t count;
  17.     fp = fopen("primes.txt", "w+");
  18. for (i =3; i <1000000000; i=i+2) {
  19. fseek (fp,0,SEEK_END);
  20. if (ftell(fp)<4000000000) {
  21. if (i%3!=0 || i==3) {
  22. if (i%5!=0 || i==5) {
  23. if (i%7!=0 || i==7) {
  24. if (i%11!=0 || i==11) {
  25. if (i%13!=0 || i==13) {
  26. if (i%17!=0 || i==17) {
  27. if (i%19!=0 || i==19) {
  28. if (i%23!=0 || i==23) {
  29. if (i%29!=0 || i==29) {
  30. if (i%31!=0 || i==31) {
  31. if (i%37!=0 || i==37) {
  32. if (i%41!=0 || i==41) {
  33. if (i%43!=0 || i==43) {
  34. if (i%47!=0 || i==47) {
  35. if (i%53!=0 || i==53) {
  36. if (i%59!=0 || i==59) {
  37. if (i%61!=0 || i==61) {
  38. if (i%67!=0 || i==67) {
  39. if (i%71!=0 || i==71) {
  40. if (i%73!=0 || i==73) {
  41. if (i%79!=0 || i==79) {
  42. if (i%83!=0 || i==83) {
  43. if (i%89!=0 || i==89) {
  44. if (i%97!=0 || i==97) {
  45. if (i%101!=0 || i==101) {
  46. if (i%103!=0 || i==103) {
  47. if (i%107!=0 || i==107) {
  48. if (i%109!=0 || i==109) {
  49. if (i%113!=0 || i==113) {
  50. if (i%127!=0 || i==127) {
  51. if (i%131!=0 || i==131) {
  52. if (i%137!=0 || i==137) {
  53. if (i%139!=0 || i==139) {
  54. if (i%149!=0 || i==149) {
  55. if (i%151!=0 || i==151) {
  56. if (i%157!=0 || i==157) {
  57. if (i%163!=0 || i==163) {
  58. if (i%167!=0 || i==167) {
  59. if (i%173!=0 || i==173) {
  60. if (i%179!=0 || i==179) {
  61. if (i%181!=0 || i==181) {
  62. if (i%191!=0 || i==191) {
  63. if (i%193!=0 || i==193) {
  64. if (i%197!=0 || i==197) {
  65. if (i%199!=0 || i==199) {
  66. if (i%211!=0 || i==211) {
  67. if (i%223!=0 || i==223) {
  68. if (i%227!=0 || i==227) {
  69. if (i%229!=0 || i==229) {
  70. if (i%233!=0 || i==233) {
  71. if (i%239!=0 || i==239) {
  72. if (i%241!=0 || i==241) {
  73. if (i%251!=0 || i==251) {
  74. if (i%257!=0 || i==257) {
  75. if (i%263!=0 || i==263) {
  76. if (i%269!=0 || i==269) {
  77. if (i%271!=0 || i==271) {
  78. if (i%277!=0 || i==277) {
  79. if (i%281!=0 || i==281) {
  80. if (i%283!=0 || i==283) {
  81. if (i%293!=0 || i==293) {
  82. if (i%307!=0 || i==307) {
  83. if (i%311!=0 || i==311) {
  84. if (i%313!=0 || i==313) {
  85. if (i%317!=0 || i==317) {
  86. if (i%331!=0 || i==331) {
  87. if (i%337!=0 || i==337) {
  88. if (i%347!=0 || i==347) {
  89. if (i%349!=0 || i==349) {
  90. if (i%353!=0 || i==353) {
  91. if (i%359!=0 || i==359) {
  92. if (i%367!=0 || i==367) {
  93. if (i%373!=0 || i==373) {
  94. if (i%379!=0 || i==379) {
  95. if (i%383!=0 || i==383) {
  96. if (i%389!=0 || i==389) {
  97. if (i%397!=0 || i==397) {
  98. if (i%401!=0 || i==401) {
  99. if (i%409!=0 || i==409) {
  100. if (i%419!=0 || i==419) {
  101. if (i%421!=0 || i==421) {
  102. if (i%431!=0 || i==431) {
  103. if (i%433!=0 || i==433) {
  104. if (i%439!=0 || i==439) {
  105. if (i%443!=0 || i==443) {
  106. if (i%449!=0 || i==449) {
  107. if (i%457!=0 || i==457) {
  108. if (i%461!=0 || i==461) {
  109. if (i%463!=0 || i==463) {
  110. if (i%467!=0 || i==467) {
  111. if (i%479!=0 || i==479) {
  112. if (i%487!=0 || i==487) {
  113. if (i%491!=0 || i==491) {
  114. if (i%499!=0 || i==499) {
  115. if (i%503!=0 || i==503) {
  116. if (i%509!=0 || i==509) {
  117. if (i%521!=0 || i==521) {
  118. if (i%523!=0 || i==523) {
  119. if (i%541!=0 || i==541) {
  120. if (i%547!=0 || i==547) {
  121. if (i%557!=0 || i==557) {
  122. if (i%563!=0 || i==563) {
  123. if (i%569!=0 || i==569) {
  124. if (i%571!=0 || i==571) {
  125. if (i%577!=0 || i==577) {
  126. if (i%587!=0 || i==587) {
  127. if (i%593!=0 || i==593) {
  128. if (i%599!=0 || i==599) {
  129. if (i%601!=0 || i==601) {
  130. if (i%607!=0 || i==607) {
  131. if (i%613!=0 || i==613) {
  132. if (i%617!=0 || i==617) {
  133. if (i%619!=0 || i==619) {
  134. if (i%631!=0 || i==631) {
  135. if (i%641!=0 || i==641) {
  136. if (i%643!=0 || i==643) {
  137. if (i%647!=0 || i==647) {
  138. if (i%653!=0 || i==653) {
  139. if (i%659!=0 || i==659) {
  140. if (i%661!=0 || i==661) {
  141. if (i%673!=0 || i==673) {
  142. if (i%677!=0 || i==677) {
  143. if (i%683!=0 || i==683) {
  144. if (i%691!=0 || i==691) {
  145. if (i%701!=0 || i==701) {
  146. if (i%709!=0 || i==709) {
  147. if (i%719!=0 || i==719) {
  148. if (i%727!=0 || i==727) {
  149. if (i%733!=0 || i==733) {
  150. if (i%739!=0 || i==739) {
  151. if (i%743!=0 || i==743) {
  152. if (i%751!=0 || i==751) {
  153. if (i%757!=0 || i==757) {
  154. if (i%761!=0 || i==761) {
  155. if (i%769!=0 || i==769) {
  156. if (i%773!=0 || i==773) {
  157. if (i%787!=0 || i==787) {
  158. if (i%797!=0 || i==797) {
  159. if (i%809!=0 || i==809) {
  160. if (i%811!=0 || i==811) {
  161. if (i%821!=0 || i==821) {
  162. if (i%823!=0 || i==823) {
  163. if (i%827!=0 || i==827) {
  164. if (i%829!=0 || i==829) {
  165. if (i%839!=0 || i==839) {
  166. if (i%853!=0 || i==853) {
  167. if (i%857!=0 || i==857) {
  168. if (i%859!=0 || i==859) {
  169. if (i%863!=0 || i==863) {
  170. if (i%877!=0 || i==877) {
  171. if (i%881!=0 || i==881) {
  172. if (i%883!=0 || i==883) {
  173. if (i%887!=0 || i==887) {
  174. if (i%907!=0 || i==907) {
  175. if (i%911!=0 || i==911) {
  176. if (i%919!=0 || i==919) {
  177. if (i%929!=0 || i==929) {
  178. if (i%937!=0 || i==937) {
  179. if (i%941!=0 || i==941) {
  180. if (i%947!=0 || i==947) {
  181. if (i%953!=0 || i==953) {
  182. if (i%967!=0 || i==967) {
  183. if (i%971!=0 || i==971) {
  184. if (i%977!=0 || i==977) {
  185. if (i%991!=0 || i==991) {
  186. if (i%997!=0 || i==997) {
  187. if (i%1009!=0 || i==1009) {
  188. if (i%1013!=0 || i==1013) {
  189. if (i%1019!=0 || i==1019) {
  190. if (i%1021!=0 || i==1021) {
  191. if (i%1031!=0 || i==1031) {
  192. if (i%1033!=0 || i==1033) {
  193. if (i%1039!=0 || i==1039) {
  194. if (i%1049!=0 || i==1049) {
  195. if (i%1051!=0 || i==1051) {
  196. if (i%1061!=0 || i==1061) {
  197. if (i%1063!=0 || i==1063) {
  198. if (i%1069!=0 || i==1069) {
  199. if (i%1087!=0 || i==1087) {
  200. if (i%1091!=0 || i==1091) {
  201. if (i%1093!=0 || i==1093) {
  202. if (i%1097!=0 || i==1097) {
  203. if (i%1103!=0 || i==1103) {
  204. if (i%1109!=0 || i==1109) {
  205. if (i%1117!=0 || i==1117) {
  206. if (i%1123!=0 || i==1123) {
  207. if (i%1129!=0 || i==1129) {
  208. if (i%1151!=0 || i==1151) {
  209. if (i%1153!=0 || i==1153) {
  210. if (i%1163!=0 || i==1163) {
  211. if (i%1171!=0 || i==1171) {
  212. if (i%1181!=0 || i==1181) {
  213. if (i%1187!=0 || i==1187) {
  214. if (i%1193!=0 || i==1193) {
  215. if (i%1201!=0 || i==1201) {
  216. if (i%1213!=0 || i==1213) {
  217. if (i%1217!=0 || i==1217) {
  218. if (i%1223!=0 || i==1223) {
  219. if (i%1229!=0 || i==1229) {
  220. if (i%1231!=0 || i==1231) {
  221. if (i%1237!=0 || i==1237) {
  222. if (i%1249!=0 || i==1249) {
  223. if (i%1259!=0 || i==1259) {
  224. if (i%1277!=0 || i==1277) {
  225. if (i%1279!=0 || i==1279) {
  226. if (i%1283!=0 || i==1283) {
  227. if (i%1289!=0 || i==1289) {
  228. if (i%1291!=0 || i==1291) {
  229. if (i%1297!=0 || i==1297) {
  230. if (i%1301!=0 || i==1301) {
  231. if (i%1303!=0 || i==1303) {
  232. if (i%1307!=0 || i==1307) {
  233. if (i%1319!=0 || i==1319) {
  234. if (i%1321!=0 || i==1321) {
  235. if (i%1327!=0 || i==1327) {
  236. if (i%1361!=0 || i==1361) {
  237. if (i%1367!=0 || i==1367) {
  238. if (i%1373!=0 || i==1373) {
  239. if (i%1381!=0 || i==1381) {
  240. if (i%1399!=0 || i==1399) {
  241. if (i%1409!=0 || i==1409) {
  242. if (i%1423!=0 || i==1423) {
  243. if (i%1427!=0 || i==1427) {
  244. if (i%1429!=0 || i==1429) {
  245. if (i%1433!=0 || i==1433) {
  246. if (i%1439!=0 || i==1439) {
  247. if (i%1447!=0 || i==1447) {
  248. if (i%1451!=0 || i==1451) {
  249. if (i%1453!=0 || i==1453) {
  250. if (i%1459!=0 || i==1459) {
  251. if (i%1471!=0 || i==1471) {
  252. if (i%1481!=0 || i==1481) {
  253. if (i%1483!=0 || i==1483) {
  254. if (i%1487!=0 || i==1487) {
  255. if (i%1489!=0 || i==1489) {
  256. if (i%1493!=0 || i==1493) {
  257. if (i%1499!=0 || i==1499) {
  258. if (i%1511!=0 || i==1511) {
  259. if (i%677!=0 || i==677) {
  260. if (i%683!=0 || i==683) {
  261. if (i%691!=0 || i==691) {
  262. if (i%701!=0 || i==701) {
  263. if (i%3!=0 || i==3) {
  264. if (i%5!=0 || i==5) {
  265. if (i%7!=0 || i==7) {
  266. if (i%11!=0 || i==11) {
  267. if (i%13!=0 || i==13) {
  268. if (i%17!=0 || i==17) {
  269. if (i%19!=0 || i==19) {
  270. if (i%23!=0 || i==23) {
  271. if (i%29!=0 || i==29) {
  272. if (i%31!=0 || i==31) {
  273. if (i%37!=0 || i==37) {
  274. if (i%41!=0 || i==41) {
  275. if (i%43!=0 || i==43) {
  276. if (i%47!=0 || i==47) {
  277. if (i%53!=0 || i==53) {
  278. if (i%59!=0 || i==59) {
  279. if (i%61!=0 || i==61) {
  280. if (i%67!=0 || i==67) {
  281. if (i%71!=0 || i==71) {
  282. if (i%73!=0 || i==73) {
  283. if (i%79!=0 || i==79) {
  284. if (i%83!=0 || i==83) {
  285. if (i%89!=0 || i==89) {
  286. if (i%97!=0 || i==97) {
  287. if (i%101!=0 || i==101) {
  288. if (i%103!=0 || i==103) {
  289. if (i%107!=0 || i==107) {
  290. if (i%109!=0 || i==109) {
  291. if (i%113!=0 || i==113) {
  292. if (i%127!=0 || i==127) {
  293. if (i%131!=0 || i==131) {
  294. if (i%137!=0 || i==137) {
  295. if (i%139!=0 || i==139) {
  296. if (i%149!=0 || i==149) {
  297. if (i%151!=0 || i==151) {
  298. if (i%157!=0 || i==157) {
  299. if (i%163!=0 || i==163) {
  300. if (i%167!=0 || i==167) {
  301. if (i%173!=0 || i==173) {
  302. if (i%179!=0 || i==179) {
  303. if (i%181!=0 || i==181) {
  304. if (i%191!=0 || i==191) {
  305. if (i%193!=0 || i==193) {
  306. if (i%197!=0 || i==197) {
  307. if (i%199!=0 || i==199) {
  308. if (i%211!=0 || i==211) {
  309. if (i%223!=0 || i==223) {
  310. if (i%227!=0 || i==227) {
  311. if (i%229!=0 || i==229) {
  312. if (i%233!=0 || i==233) {
  313. if (i%239!=0 || i==239) {
  314. if (i%241!=0 || i==241) {
  315. if (i%251!=0 || i==251) {
  316. if (i%257!=0 || i==257) {
  317. if (i%263!=0 || i==263) {
  318. if (i%269!=0 || i==269) {
  319. if (i%271!=0 || i==271) {
  320. if (i%277!=0 || i==277) {
  321. if (i%281!=0 || i==281) {
  322. if (i%283!=0 || i==283) {
  323. if (i%293!=0 || i==293) {
  324. if (i%307!=0 || i==307) {
  325. if (i%311!=0 || i==311) {
  326. if (i%313!=0 || i==313) {
  327. if (i%317!=0 || i==317) {
  328. if (i%331!=0 || i==331) {
  329. if (i%337!=0 || i==337) {
  330. if (i%347!=0 || i==347) {
  331. if (i%349!=0 || i==349) {
  332. if (i%353!=0 || i==353) {
  333. if (i%359!=0 || i==359) {
  334. if (i%367!=0 || i==367) {
  335. if (i%373!=0 || i==373) {
  336. if (i%379!=0 || i==379) {
  337. if (i%383!=0 || i==383) {
  338. if (i%389!=0 || i==389) {
  339. if (i%397!=0 || i==397) {
  340. if (i%401!=0 || i==401) {
  341. if (i%409!=0 || i==409) {
  342. if (i%419!=0 || i==419) {
  343. if (i%421!=0 || i==421) {
  344. if (i%431!=0 || i==431) {
  345. if (i%433!=0 || i==433) {
  346. if (i%439!=0 || i==439) {
  347. if (i%443!=0 || i==443) {
  348. if (i%449!=0 || i==449) {
  349. if (i%457!=0 || i==457) {
  350. if (i%461!=0 || i==461) {
  351. if (i%463!=0 || i==463) {
  352. if (i%467!=0 || i==467) {
  353. if (i%479!=0 || i==479) {
  354. if (i%487!=0 || i==487) {
  355. if (i%491!=0 || i==491) {
  356. if (i%499!=0 || i==499) {
  357. if (i%503!=0 || i==503) {
  358. if (i%509!=0 || i==509) {
  359. if (i%521!=0 || i==521) {
  360. if (i%523!=0 || i==523) {
  361. if (i%541!=0 || i==541) {
  362. if (i%547!=0 || i==547) {
  363. if (i%557!=0 || i==557) {
  364. if (i%563!=0 || i==563) {
  365. if (i%569!=0 || i==569) {
  366. if (i%571!=0 || i==571) {
  367. if (i%577!=0 || i==577) {
  368. if (i%587!=0 || i==587) {
  369. if (i%593!=0 || i==593) {
  370. if (i%599!=0 || i==599) {
  371. if (i%601!=0 || i==601) {
  372. if (i%607!=0 || i==607) {
  373. if (i%613!=0 || i==613) {
  374. if (i%617!=0 || i==617) {
  375. if (i%619!=0 || i==619) {
  376. if (i%631!=0 || i==631) {
  377. if (i%641!=0 || i==641) {
  378. if (i%643!=0 || i==643) {
  379. if (i%647!=0 || i==647) {
  380. if (i%653!=0 || i==653) {
  381. if (i%659!=0 || i==659) {
  382. if (i%661!=0 || i==661) {
  383. if (i%673!=0 || i==673) {
  384. if (i%677!=0 || i==677) {
  385. if (i%683!=0 || i==683) {
  386. if (i%691!=0 || i==691) {
  387. if (i%701!=0 || i==701) {
  388. if (i%709!=0 || i==709) {
  389. if (i%719!=0 || i==719) {
  390. if (i%727!=0 || i==727) {
  391. if (i%733!=0 || i==733) {
  392. if (i%739!=0 || i==739) {
  393. if (i%743!=0 || i==743) {
  394. if (i%751!=0 || i==751) {
  395. if (i%757!=0 || i==757) {
  396. if (i%761!=0 || i==761) {
  397. if (i%769!=0 || i==769) {
  398. if (i%773!=0 || i==773) {
  399. if (i%787!=0 || i==787) {
  400. if (i%797!=0 || i==797) {
  401. if (i%809!=0 || i==809) {
  402. if (i%811!=0 || i==811) {
  403. if (i%821!=0 || i==821) {
  404. if (i%823!=0 || i==823) {
  405. if (i%827!=0 || i==827) {
  406. if (i%829!=0 || i==829) {
  407. if (i%839!=0 || i==839) {
  408. if (i%853!=0 || i==853) {
  409. if (i%857!=0 || i==857) {
  410. if (i%859!=0 || i==859) {
  411. if (i%863!=0 || i==863) {
  412. if (i%877!=0 || i==877) {
  413. if (i%881!=0 || i==881) {
  414. if (i%883!=0 || i==883) {
  415. if (i%887!=0 || i==887) {
  416. if (i%907!=0 || i==907) {
  417. if (i%911!=0 || i==911) {
  418. if (i%919!=0 || i==919) {
  419. if (i%929!=0 || i==929) {
  420. if (i%937!=0 || i==937) {
  421. if (i%941!=0 || i==941) {
  422. if (i%947!=0 || i==947) {
  423. if (i%953!=0 || i==953) {
  424. if (i%967!=0 || i==967) {
  425. if (i%971!=0 || i==971) {
  426. if (i%977!=0 || i==977) {
  427. if (i%991!=0 || i==991) {
  428. if (i%997!=0 || i==997) {
  429. if (i%1009!=0 || i==1009) {
  430. fprintf(fp, "%d,",i);
  431. prime=prime+1;
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. }
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. }
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }
  645. }
  646. }
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. }
  687. }
  688. }
  689. }
  690. }
  691. }
  692. }
  693. }
  694. }
  695. }
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. }
  707. }
  708. }
  709. }
  710. }
  711. }
  712. }
  713. }
  714. }
  715. }
  716. }
  717. }
  718. }
  719. }
  720. }
  721. }
  722. }
  723. }
  724. }
  725. }
  726. }
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. }
  734. }
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. }
  765. }
  766. }
  767. }
  768. }
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. }
  778. }
  779. }
  780. }
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }
  787. }
  788. }
  789. }
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. }
  797. }
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. }
  805. }
  806. }
  807. }
  808. }
  809. }
  810. }
  811. }
  812. }
  813. }
  814. }
  815. }
  816. }
  817. }
  818. }
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. }
  826. }
  827. }
  828. }
  829. }
  830. }
  831. }
  832. }
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. }
  841. }
  842. else
  843. {
  844. fp=fopen("primes1.txt","w");
  845. }
  846. }
  847. printf("%d\n",prime);
  848. system("pause");
  849.     return 0;
  850. }
  851. //---------------------------------------------------------------------------
  852.  
  853. //---------------------------------------------------------------------------
Aug 14 '09 #1
98 6042
JosAH
11,448 Expert 8TB
You're kidding us right? Nobody in his/her right mind writes code like that. Have a look at this:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. static int isPrime(int n) {
  4.  
  5.     int i, inc= 2;
  6.  
  7.     if (n == 2 || n == 3) return 1;
  8.     if (n%2 == 0 || n%3 == 0) return 0;
  9.  
  10.     for (i= 5; i*i <= n; i+= inc, inc= 6-inc)
  11.         if (!(n%i)) return 0;
  12.  
  13.     return 1;
  14. }
  15.  
  16. int main() {
  17.  
  18.     int i;
  19.  
  20.     for (i= 2; i < 10000; i++)
  21.         if (isPrime(i))
  22.             printf("%d\n");
  23.  
  24.     return 0;
  25. }
  26.  
kind regards,

Jos
Aug 14 '09 #2
don't even understand that maybe that's why I never made it like that
Aug 14 '09 #3
Also Jos I'm looking for a loop to shorten it not a sidenote of how crazy i am
Aug 14 '09 #4
donbock
2,426 Expert 2GB
@scienceman88
I assume you're asking for a way to shorten the nest of if-statements. You could replace all of those if statements with a loop that starts at 3, increments by 2, and stops when the counter exceeds the square root of the number you're testing. That's not necessarily the best algorithm, but it is equivalent to the one you're using.
Aug 15 '09 #5
Banfa
9,065 Expert Mod 8TB
So you wouldn't call Jos loop rather shorter than your code then?
Aug 15 '09 #6
Banfa
9,065 Expert Mod 8TB
As an aside an alternitive to using a function to test to see if a number is prime if you are looking for primes in a given range of numbers is to just remove all the none primes from the list, something like

Expand|Select|Wrap|Line Numbers
  1. Populate LIST will all integers from 2 to 18 Quintillion
  2.  
  3. LOOP
  4.     Get next NUMBER in list
  5.     Remove all multiples of NUMBER except NUMBER*1 from the list
  6. UNTIL END IS REACHED
  7.  
At the end of the algorithm you have a list that just contains prime numbers in the range you start with. I do not know how efficient this algorithm is but I believe it isn't too in-efficient.
Aug 15 '09 #7
To test for a number whether it's a prime or not is "undecidable," and in terms of time efficiency, I think it's NP-Hard; NP stands for None-polynomial.

Odd factors? I don't exactly got what you mean by this!

In the below discussion, I'm using the Dynamic Programming technique.

To shorten the loop you are writing,
1. start with the 2 and 3 as the prime numbers, by definition.
2-a. get the next prime number,
2-b. add to the list of sentinels
3. process: do what ever you want.

So, talking about step 2 above, an efficient, but none-optimized, algorithm is the following, here is the "basic" idea.

From now on, I'll be using RO to stand for "Room for Optimization," and OT for "Optimization Technique being used/adopted."

Expand|Select|Wrap|Line Numbers
  1. /* define your list of sentinels, this BASIS list might change according to the OT. */
  2.  
  3. int sentinels[] =  {2, 3}  /* According to OT, 0 and 1 might be added */
  4. int k = 2; /* the next avail slot in sentinels */
  5. /* sentinels will be re-enlarged, re-allocate space using ralloc(), but less efficient to be re-allocated in the below function  */
  6. function is_prime(integral_type n)
  7. {
  8.   /* k might be declared as static instead of being global above */
  9.   if (n in sentinels)
  10.     return TRUE;
  11.   /* compute the square root of n */
  12.   i = 3; /* RO */
  13.   r = floor(square_root(n)); /* RO */
  14.   while (i <= r && n % i) i++; /* Change of OT, then RO */
  15.   if ( i < r) /* premature */
  16.     return FALSE;
  17.   sentinels[k++] = n;
  18.   return TRUE;
  19. }
  20.  
Optimization fine point. The next available prime number is completely independent of the immediately preceding one and can never be used to test for the next available one. As thus, no room for optimization, but some improvement might be done -- no proof, ONLY ad hod -- for a specific problem domain, this sometimes works, but not absolute.

List of Sentinels. This list is incremental, need some specific point in the whole program as to be doubled in size, at some point this can't be done, due to the much wasted space, alternatively, some "predetermined" size, OT, can be added each time a space is needed.

Finding multiples of a prime and exclude them from testing.
Hope this helps out.
Aug 15 '09 #8
donbock
2,426 Expert 2GB
@scienceman88
To test if a particular candidate number is prime you need only check if any of the prime numbers less than or equal to the square root of the candidate are factors. Nothing is gained by checking for nonprime factors; nothing is gained by checking for factors greater than the square root of the candidate. However, the effort to distinguish prime factors from nonprime factors is not trivial.

If you step through Jos' code you will notice that the trickiness involving inc causes only factors that are not divisible by 2 or 3 (other than 2 and 3 themselves) to be checked against the candidate. That is, Jos prunes out a small subset of the nonprime factors in order to speed up the code.

You could try to prune out a larger subset of nonprime factors, but at some point the additional pruning overhead swamps the savings from skipping factors.
Aug 15 '09 #9
donbock
2,426 Expert 2GB
@hsheboul
The sentinels array is defined as having only two slots. The next prime added to the array (5) will be written past the end of the array -- wreaking havoc on the program. You either want to dynamically allocate sentinels and periodically realloc it or you want a large static allocation. For instance, if you know ahead of time that you intend to search for all primes less than N, you could define sentinels as follows (where Y = square root of N + 1; I'm too lazy to check if the +1 is actually needed):
Expand|Select|Wrap|Line Numbers
  1. int sentinels[Y] = {2,3};
Note: you have to compute Y yourself and type in the literal number; the compiler can't do it for you.
Aug 15 '09 #10
@donbock
You are right Don, I was not that precise in writing the pseudo code.

@donbock
Ah, how large is that N, and how to find some info related to that N!

Two things can decide upon N:
(don't forget the problem is undecidable)
1. the hardware capabilities,
2. the time available

Check out
http://primes.utm.edu/largest.html
Aug 15 '09 #11
JosAH
11,448 Expert 8TB
@scienceman88
Tadaa! Lines #10 and #11 are the loop you asked for. All primes (except 2 and 3) are a multiple of 6 plus or minus 1; that's exactly what that loop tests: i.e. if a number evenly divides by 6*x +- 1 it can never be a prime number.

kind regards,

Jos
Aug 15 '09 #12
Banfa
9,065 Expert Mod 8TB
@JosAH
[slightly off topic silly and pedantic mode]Strictly speaking if a number is not a prime number it can "never" be a prime number there time is not a factor controlling if a number is prime or not :D
[/slightly off topic silly and pedantic mode]

Err Sorry
Aug 15 '09 #13
JosAH
11,448 Expert 8TB
@Banfa
You're wrong: there can be temporary prime numbers; e.g. they can be prime during the weekends or after 11:00pm or so. My hypothesis is that every number can be a temporary prime number but we don't know when it'll be prime and maybe they're also shy so they won't show their primeness when they're prime ...

kind regards,

Jos ;-)
Aug 15 '09 #14
Okay
1)I barely understood your code jos
2)I tried to insert it to file instead and I couldn't get it to work.
3)N= max for unsigned long long integers or about 18,446,744,073,709,551,615
4) you can use prime numbers before to calculate the next one it follows the sieve of eratosthenes( i know the sieve of atkin is faster but I don't understand it) and those sieves are the only known ways to find all primes except maybe primality testing each number
Aug 15 '09 #15
5)I want to use ftell and f seek to keep the files small enough to read in dos 4GB(a little less for possible overflow) I think so
Aug 15 '09 #16
JosAH
11,448 Expert 8TB
@scienceman88
1) Yes I know, I've always been a smartypants; my mother dropped me on my head when I was a baby ;-)

2) My code simply prints the primes to stdout; it doesn't take rocket science to print the primes to elsewehere.

3) I take your word for it; no, I won't I'll check it ... yep, you're right.

4) both are similar algorithms in disguise: sieves filter out equal factors for all numbers while my primality check filtes out all factors per number. It doesn't need that huge array of bits to keep track of all the numbers.

kind regards.

Jos
Aug 15 '09 #17
JosAH
11,448 Expert 8TB
@scienceman88
Just keep track on the number of bytes written already; when it exceeds a certain threshold, close the file and quit the process. There's no need for ftell() or fseek().

kind regards,

Jos
Aug 15 '09 #18
@JosAH
unless I watch it manually close it and expect it to say next file there is as i know it won't all get into one file of 4 gb or less
Aug 15 '09 #19
I can't get it to print to file never worked in more than one function before so unless i have to declare the pointer globally but change it internally I'll never be able to use the code
Aug 15 '09 #20
JosAH
11,448 Expert 8TB
@scienceman88
Think 'object based': the 'object' or 'module' takes care of the printing and keeps track of that threshold value; whenever the number of bytes written exceeds that value, close the file, open a next one and reset the number of bytes written. At the end close the last file.

kind regards,

Jos
Aug 15 '09 #21
I have no Idea how to josah
Aug 15 '09 #22
JosAH
11,448 Expert 8TB
@scienceman88
That is not very a scientific attitude (despite the suggestion of your name). Can you come up with a function that returns a C string "file0", "file1", "file2" ... everytime you call it? If so you're halfway there ...

kind regards,

Jos
Aug 15 '09 #23
I have one in my original code but I can't even get the file part to work in your code so changing files is pointless also without fseek and ftell I don't know how to track the size of the file as for my name it's because I like science not people like you.
Aug 15 '09 #24
JosAH
11,448 Expert 8TB
@scienceman88
Ok, I'll stop answering your questions then but mind you: nobody here is going to do your homework for you; we don't believe in that approach. You have to do your work and when you're stuck we can try to help you.

Bye bye.

Jos
Aug 15 '09 #25
JosAH
11,448 Expert 8TB
@hsheboul
That test most certainly is a decidable problem. It may be a bit tough for large numbers but that's it. For any integral n > 0 I can decide in O(n) whether or not n is a prime number; it isn't even in any NP class of problems.

kind regards,

Jos
Aug 15 '09 #26
I'm not trying to sound mean but the best I've done in understanding you is the original code you posted that doesn't seem to allow a file pointer anywhere where it isn't global(and because it had no comments it took me 30 minutes( and a pasting into my compiler) to figure out as I've never made a program with more than one function) also nobody asked how long I've been using C I've been barely able to do anything and I've been at it less than a full 3 months only thing that helped me learn printing at all is codeguru.com and learning a bit about pointers ( obviously not enough) basically I want something like this:

if (i%x!=0 && x<=sqrt(i)){
x=x+2;
}

and if that shows to be false for all odd numbers under the sqrt(i) and the file size is not bigger than 4 gb then print it to primesx.txt and increase my primes variable by one.
Aug 15 '09 #27
JosAH
11,448 Expert 8TB
@scienceman88
Here's a nice reference. Everywhere where I use printf() you can use fprintf() and use a file pointer (which you have opened somewhere before in your code). You have to learn how to crawl before you can walk; you have to learn how to walk before you can run ...

kind regards,

Jos
Aug 15 '09 #28
when i tried something as simple as this it fails as it goes above 25 the first square number greater than 3^2 that will be incountered if i=i+2 and i starts at 3
Aug 15 '09 #29
@JosAH
I only picked this name because I like science and crap_programmer and crappy programmer are already taken.
Aug 15 '09 #30
donbock
2,426 Expert 2GB
@scienceman88
OK. You started working with C less than 3 months ago. Let's get some further background.
  • Do you have a C mentor you can talk to face-to-face (teacher, fellow student, coworker)?
  • How did you come up with the idea to find all primes that fit in a long long int?
  • Do you have proficiency in any other computer languages or is C your first?
  • Have you accomplished file I/O before this prime problem?
  • Have you successfully used command line arguments (argv and argc)?
It sounds like file I/O is giving you the most trouble. Is that right? It sounds like you've reworked your program quite a bit to try to incorporate Jos' suggestion. It might help if you posted the current version and precsely described what is going wrong with it. Provide compiler and run-time error messages verbatim. If the program output is wrong, clearly describe how the actual output differs from the expected output.
Aug 15 '09 #31
JosAH
11,448 Expert 8TB
@scienceman88
That is exactly what my isPrime() function does: it explicitly checks for the numbers 2 and 3 and then it loops over all numbers 6*x +/- 1; call that number 'i'; it loops for all 'i' where 'i*i <= n' or, as you put it: less than sqrt(n). My code simply avoids that sqrt( ... ) function call and it avoids spurious tests by simply checking for possible divisors of the form 6*x +/- 1.

There is no file fiddling in the isPrime() function, the printing is done outside of that function; they call that "separation of concerns" which is a good thing.

kind regards,

Jos
Aug 15 '09 #32
donbock
2,426 Expert 2GB
@scienceman88
I think of computer programming more as engineering than science. One of the primary strategies in engineering is decomposition, also called separation of concerns or divide-and-conquer. Break up the task into small enough subtasks such that each has a straightforward implementation and so that each can be tested in isolation.

I submit that the most important thing for you to learn while working out this prime problem is how to divide a computer program into functions.

How small should the subtasks be? To misquote Albert Einstein, "as small as necessary but no smaller".

What should be the relationships between the subtasks? Now you're getting into the realm of computer science. Refer to Coupling and Cohesion. As a neophyte you needn't get too worked up over these terms, but it is never too early to follow good engineering principles.
Aug 15 '09 #33
thank you josah I'm sorry I've never truely been diagnosed but since about ten years ago I've been told I might have autism which if I have done my research and understand it is a high iq social disorder that fits me as far as talking about subjects i like in conversation I like science get me on a science topic I know and you won't get me off it lol ( somewhat the same in math) maybe I'd do better talking binary lol.
Aug 15 '09 #34
I'm checking how big can fit into one file right now I've done vb.net before which I was helped into no I don't have a C mentor and most things I found until this site seem to be C++ or just don't seem to work for me. the idea came from when i tried to do it in vb.net. I'm not good but I have tried input and output a very small amount in C since I started learning in fact because I know it can be useful it was one of the first things i searched for outside basics. I've used system(); but not really argc or argv I've seen them in codes but know nothing about them.
Aug 15 '09 #35
well although I know of them I don't know much about the file input and output commands I only learned them from codeguru.com and after I learned pointer basics(which I'll need more of) they made a little more sense ( made more sense when i saw a code that instead of using const char* _blah blah I found code saying const *char _pathname
Aug 15 '09 #36
JosAH
11,448 Expert 8TB
Ok, let's get on with the show: the chance of a number being prime is inversely proportional to the number of digits in that number, or roughly 1/log(n). Some sloppy math shows that for n numbers there are about n/log(n) primes. Substitute 2^64 for n and you get the number of primes that fit in a long long. On average a long long takes up about 19 digits so all your primes take 19*2^64/64 bytes (written in ASCII). That is way too much to store on disk.

kind regards,

Jos
Aug 15 '09 #37
is there a statement to change type of variable or do i have to use void if so I could change it's type as i go and save space that way
Aug 15 '09 #38
JosAH
11,448 Expert 8TB
@scienceman88
Nope, even when you use just eight bytes per number you end up with 2^64/64 bytes (give or take a bit) which is also way too much to store on disk. It can't be really compressed because the distribution of primes is considered random. It just can't be done. You have to limit yourself to unsigned long; it can handle numbers in the range [0 ... 2^32-1] which is still quite a lot ...

Let's do the (sloppy) calculation again for unsigned long: it contains n/log(n) prime numbers so an unsigned long can contain 2^32/5 prime numbers; a long takes up about 8 or 9 digits on average so all the prime numbers take up something like 2^32/5*8 == 2^35/5 bytes of storage.

kind regards,

Jos
Aug 15 '09 #39
donbock
2,426 Expert 2GB
@scienceman88
A variable is declared/defined with a particular type. There is no way to change the type on the fly. Why would you want to do so?

It sounds like you think you can reduce the size of the output file(s) by altering the type of the prime candidate variable on the fly. The type of the prime candidate variable is irrelevant. The size of the output file is determined by what is written to it. Jos assumed you want to write the decimal-ASCII representation of each prime to the file (he estimated that the average number of digits across all lines in the file would be 19). If you want to reduce the size of the file you need to either store fewer values in it or encode each value in fewer bytes -- you can roughly cut the file size in half if you represent each prime value as an 8-byte binary value.

You might argue that smaller prime values can fit in fewer bytes. The problem with variable-length entries is that it will be much harder to recognize the dividing line between one value and another. There are ways to do this; but you will have to be particularly clever to come out ahead.
Aug 15 '09 #40
JosAH
11,448 Expert 8TB
@donbock
That still doesn't cut it ;-) do the calculations: 8*2^64/64 == 2^64/8 == 2^61 is still way too much.

kind regards,

Jos ;-)
Aug 15 '09 #41
I have a lot of google groups could I ftp to them ? I could fit at least 70 GB there even if it is compressed and a further 200 GB on my hard drive and maybe even a further 3-4 on my portable drive before I run out of room I might need to run it on a 64 bit computer to go that high anybody up for the challenge ? I calculated that as a long long I can fit over 20 billion primes on my computer uncompressed in normal ASCII form which means up to 40 billion primes uncompressed in the binary form you say will cut the size in half
Aug 15 '09 #42
donbock
2,426 Expert 2GB
I suggest you first get your program to produce a list of all the prime numbers less than 100. That list is short enough for you to hand-check the results against a reference book to confirm the program works properly.

After that, I suggest you modify the upper limit and produce a list of all the prime numbers less than ULONG_MAX (see limits.h). You should spot-check a few values in your output and also in your reference book to gain confidence every output value is a prime and that all primes are in the output.

After that, you can continue to increase the upper limit if you wish in order to enter long long int territory.

Make sure that your program tests each file I/O call for failure.
Aug 15 '09 #43
I've went all the way to 1 million before I even posted or found this site
Aug 15 '09 #44
donbock
2,426 Expert 2GB
@scienceman88
Jos estimates 2^64/64 = 2^58 bytes of storage are needed if you use an 8-byte binary representation for each prime. A gigabyte is 2^30 bytes, so this is 2^28 = 268,435,456 Gigabytes of storage -- roughly a quarter million gigabytes!

You need to set your sights lower.
Aug 15 '09 #45
donbock
2,426 Expert 2GB
@scienceman88
You might want to repeat that feat using Jos' smaller algorithm. Your original source code has a huge number of equivalence partitions.

For example, a typo on line 182 (such as "969" instead of "967") would produce errors that wouldn't be seen in any prime numbers less than 1000.

Jos' algorithm has fewer equivalence partitions, so you have a higher degree of confidence in its output after verifying a relatively small sample.
Aug 15 '09 #46
@donbock
worked to 1000 at least so now it's just a matter of space and time.
Aug 15 '09 #47
yeah that actually was one of my fears before wikipedia has the first 500. I was thinking of encoding the numbers in base 32 (0-9 and a-t I think).that could save room as numbers under 1024 only need 2 bytes at most before the seperator, and the top numbers before they go weird would only need 13 bytes not 20 that it could take.actually what i found by experimenting in the beginning is that if you go over the limits it goes either negative numbers or positive numbers relative to the next possible division of numbers so you can go past 18 quintillion you might just have to figure out what they mean to decode them.
Aug 15 '09 #48
#include <stdio.h>



static int isPrime(int n) {

int i, inc= 2;

if (n == 2 || n == 3) return 1;
if (n%2 == 0 || n%3 == 0) return 0;

for (i= 5; i*i <= n; i+= inc, inc= 6-inc)
if (!(n%i)) return 0;

return 1;
}

int main() {

int i;
FILE *fp;
fp=fopen("sample.txt","w");
for (i= 3; i < 1000; i=i+2)
if (isPrime(i))
fprintf(fp,"%d,",i);

return 0;
}
Aug 15 '09 #49
donbock
2,426 Expert 2GB
@scienceman88
Suggestions:
  • Add a line betweeen 4 and 5 to return 0 if n<2. This insures that "1" and negative inputs are handled properly; that is, they are not considered prime.
  • After line 15 you should trap an error if fp is NULL.
  • After line 18 you should trap an error if fprintf returns a negative value.
  • Before line 19 you should call fclose. You should trap an error if fclose returns a nonzero value.
"Trap an error" means print an error message to stderr and return a nonzero value from main. The standard function perror can help. Don't forget to call fclose on your way out from an fwrite error.
Aug 15 '09 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philp Smith | last post by:
Hi Does anyone have suggested code for a compact, efficient, elegant, most of all pythonic routine to produce a list of all the proper divisors of an integer (given a list of prime...
6
by: Dave | last post by:
I have to automate a process that assigns sales leads to sales people. For example: Every day we buy a list of sales leads, it ranges in size from 50 - 100 records. We have a team of sales...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
2
by: jknox13 | last post by:
Hi, I have to find the divisors of all numbers less than a number inputed by the user and state if a number is prime if no divisors. I have this code so far, but I'm getting the wrong output and...
1
by: Clint Boaz | last post by:
I am just trying to learn C++ and having a problem trying to write a program that finds the sum of the proper divisors of a given integer n and returns whether n is deficient, perfect or abundant.
3
by: jimix | last post by:
I have written the program for factors of #'s 1-50 but the factors print out below the number being factored. I can't figure out how to make it all on the same line. i.e. (now) divisors of 25 are...
7
by: jmDesktop | last post by:
From the Python.org tutorial: .... for x in range(2, n): .... if n % x == 0: .... print n, 'equals', x, '*', n/x .... break .... else: .... #...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.