loop for finding possible odd divisors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scienceman88
    New Member
    • Aug 2009
    • 85

    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

    Code:
    //---------------------------------------------------------------------------
    #include <stdlib.h>
    #include <stdio.h>
    #pragma hdrstop
    
    #include <tchar.h>
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int _tmain(int argc, _TCHAR* argv[])
    {
    unsigned long long int i;
    int fs;
    int prime=0;
    	FILE *fp;
    	size_t count;
    	fp = fopen("primes.txt", "w+");
    for (i =3; i <1000000000; i=i+2) {
    fseek (fp,0,SEEK_END);
    if (ftell(fp)<4000000000) {
    if (i%3!=0 || i==3) {
    if (i%5!=0 || i==5) {
    if (i%7!=0 || i==7) {
    if (i%11!=0 || i==11) {
    if (i%13!=0 || i==13) {
    if (i%17!=0 || i==17) {
    if (i%19!=0 || i==19) {
    if (i%23!=0 || i==23) {
    if (i%29!=0 || i==29) {
    if (i%31!=0 || i==31) {
    if (i%37!=0 || i==37) {
    if (i%41!=0 || i==41) {
    if (i%43!=0 || i==43) {
    if (i%47!=0 || i==47) {
    if (i%53!=0 || i==53) {
    if (i%59!=0 || i==59) {
    if (i%61!=0 || i==61) {
    if (i%67!=0 || i==67) {
    if (i%71!=0 || i==71) {
    if (i%73!=0 || i==73) {
    if (i%79!=0 || i==79) {
    if (i%83!=0 || i==83) {
    if (i%89!=0 || i==89) {
    if (i%97!=0 || i==97) {
    if (i%101!=0 || i==101) {
    if (i%103!=0 || i==103) {
    if (i%107!=0 || i==107) {
    if (i%109!=0 || i==109) {
    if (i%113!=0 || i==113) {
    if (i%127!=0 || i==127) {
    if (i%131!=0 || i==131) {
    if (i%137!=0 || i==137) {
    if (i%139!=0 || i==139) {
    if (i%149!=0 || i==149) {
    if (i%151!=0 || i==151) {
    if (i%157!=0 || i==157) {
    if (i%163!=0 || i==163) {
    if (i%167!=0 || i==167) {
    if (i%173!=0 || i==173) {
    if (i%179!=0 || i==179) {
    if (i%181!=0 || i==181) {
    if (i%191!=0 || i==191) {
    if (i%193!=0 || i==193) {
    if (i%197!=0 || i==197) {
    if (i%199!=0 || i==199) {
    if (i%211!=0 || i==211) {
    if (i%223!=0 || i==223) {
    if (i%227!=0 || i==227) {
    if (i%229!=0 || i==229) {
    if (i%233!=0 || i==233) {
    if (i%239!=0 || i==239) {
    if (i%241!=0 || i==241) {
    if (i%251!=0 || i==251) {
    if (i%257!=0 || i==257) {
    if (i%263!=0 || i==263) {
    if (i%269!=0 || i==269) {
    if (i%271!=0 || i==271) {
    if (i%277!=0 || i==277) {
    if (i%281!=0 || i==281) {
    if (i%283!=0 || i==283) {
    if (i%293!=0 || i==293) {
    if (i%307!=0 || i==307) {
    if (i%311!=0 || i==311) {
    if (i%313!=0 || i==313) {
    if (i%317!=0 || i==317) {
    if (i%331!=0 || i==331) {
    if (i%337!=0 || i==337) {
    if (i%347!=0 || i==347) {
    if (i%349!=0 || i==349) {
    if (i%353!=0 || i==353) {
    if (i%359!=0 || i==359) {
    if (i%367!=0 || i==367) {
    if (i%373!=0 || i==373) {
    if (i%379!=0 || i==379) {
    if (i%383!=0 || i==383) {
    if (i%389!=0 || i==389) {
    if (i%397!=0 || i==397) {
    if (i%401!=0 || i==401) {
    if (i%409!=0 || i==409) {
    if (i%419!=0 || i==419) {
    if (i%421!=0 || i==421) {
    if (i%431!=0 || i==431) {
    if (i%433!=0 || i==433) {
    if (i%439!=0 || i==439) {
    if (i%443!=0 || i==443) {
    if (i%449!=0 || i==449) {
    if (i%457!=0 || i==457) {
    if (i%461!=0 || i==461) {
    if (i%463!=0 || i==463) {
    if (i%467!=0 || i==467) {
    if (i%479!=0 || i==479) {
    if (i%487!=0 || i==487) {
    if (i%491!=0 || i==491) {
    if (i%499!=0 || i==499) {
    if (i%503!=0 || i==503) {
    if (i%509!=0 || i==509) {
    if (i%521!=0 || i==521) {
    if (i%523!=0 || i==523) {
    if (i%541!=0 || i==541) {
    if (i%547!=0 || i==547) {
    if (i%557!=0 || i==557) {
    if (i%563!=0 || i==563) {
    if (i%569!=0 || i==569) {
    if (i%571!=0 || i==571) {
    if (i%577!=0 || i==577) {
    if (i%587!=0 || i==587) {
    if (i%593!=0 || i==593) {
    if (i%599!=0 || i==599) {
    if (i%601!=0 || i==601) {
    if (i%607!=0 || i==607) {
    if (i%613!=0 || i==613) {
    if (i%617!=0 || i==617) {
    if (i%619!=0 || i==619) {
    if (i%631!=0 || i==631) {
    if (i%641!=0 || i==641) {
    if (i%643!=0 || i==643) {
    if (i%647!=0 || i==647) {
    if (i%653!=0 || i==653) {
    if (i%659!=0 || i==659) {
    if (i%661!=0 || i==661) {
    if (i%673!=0 || i==673) {
    if (i%677!=0 || i==677) {
    if (i%683!=0 || i==683) {
    if (i%691!=0 || i==691) {
    if (i%701!=0 || i==701) {
    if (i%709!=0 || i==709) {
    if (i%719!=0 || i==719) {
    if (i%727!=0 || i==727) {
    if (i%733!=0 || i==733) {
    if (i%739!=0 || i==739) {
    if (i%743!=0 || i==743) {
    if (i%751!=0 || i==751) {
    if (i%757!=0 || i==757) {
    if (i%761!=0 || i==761) {
    if (i%769!=0 || i==769) {
    if (i%773!=0 || i==773) {
    if (i%787!=0 || i==787) {
    if (i%797!=0 || i==797) {
    if (i%809!=0 || i==809) {
    if (i%811!=0 || i==811) {
    if (i%821!=0 || i==821) {
    if (i%823!=0 || i==823) {
    if (i%827!=0 || i==827) {
    if (i%829!=0 || i==829) {
    if (i%839!=0 || i==839) {
    if (i%853!=0 || i==853) {
    if (i%857!=0 || i==857) {
    if (i%859!=0 || i==859) {
    if (i%863!=0 || i==863) {
    if (i%877!=0 || i==877) {
    if (i%881!=0 || i==881) {
    if (i%883!=0 || i==883) {
    if (i%887!=0 || i==887) {
    if (i%907!=0 || i==907) {
    if (i%911!=0 || i==911) {
    if (i%919!=0 || i==919) {
    if (i%929!=0 || i==929) {
    if (i%937!=0 || i==937) {
    if (i%941!=0 || i==941) {
    if (i%947!=0 || i==947) {
    if (i%953!=0 || i==953) {
    if (i%967!=0 || i==967) {
    if (i%971!=0 || i==971) {
    if (i%977!=0 || i==977) {
    if (i%991!=0 || i==991) {
    if (i%997!=0 || i==997) {
    if (i%1009!=0 || i==1009) {
    if (i%1013!=0 || i==1013) {
    if (i%1019!=0 || i==1019) {
    if (i%1021!=0 || i==1021) {
    if (i%1031!=0 || i==1031) {
    if (i%1033!=0 || i==1033) {
    if (i%1039!=0 || i==1039) {
    if (i%1049!=0 || i==1049) {
    if (i%1051!=0 || i==1051) {
    if (i%1061!=0 || i==1061) {
    if (i%1063!=0 || i==1063) {
    if (i%1069!=0 || i==1069) {
    if (i%1087!=0 || i==1087) {
    if (i%1091!=0 || i==1091) {
    if (i%1093!=0 || i==1093) {
    if (i%1097!=0 || i==1097) {
    if (i%1103!=0 || i==1103) {
    if (i%1109!=0 || i==1109) {
    if (i%1117!=0 || i==1117) {
    if (i%1123!=0 || i==1123) {
    if (i%1129!=0 || i==1129) {
    if (i%1151!=0 || i==1151) {
    if (i%1153!=0 || i==1153) {
    if (i%1163!=0 || i==1163) {
    if (i%1171!=0 || i==1171) {
    if (i%1181!=0 || i==1181) {
    if (i%1187!=0 || i==1187) {
    if (i%1193!=0 || i==1193) {
    if (i%1201!=0 || i==1201) {
    if (i%1213!=0 || i==1213) {
    if (i%1217!=0 || i==1217) {
    if (i%1223!=0 || i==1223) {
    if (i%1229!=0 || i==1229) {
    if (i%1231!=0 || i==1231) {
    if (i%1237!=0 || i==1237) {
    if (i%1249!=0 || i==1249) {
    if (i%1259!=0 || i==1259) {
    if (i%1277!=0 || i==1277) {
    if (i%1279!=0 || i==1279) {
    if (i%1283!=0 || i==1283) {
    if (i%1289!=0 || i==1289) {
    if (i%1291!=0 || i==1291) {
    if (i%1297!=0 || i==1297) {
    if (i%1301!=0 || i==1301) {
    if (i%1303!=0 || i==1303) {
    if (i%1307!=0 || i==1307) {
    if (i%1319!=0 || i==1319) {
    if (i%1321!=0 || i==1321) {
    if (i%1327!=0 || i==1327) {
    if (i%1361!=0 || i==1361) {
    if (i%1367!=0 || i==1367) {
    if (i%1373!=0 || i==1373) {
    if (i%1381!=0 || i==1381) {
    if (i%1399!=0 || i==1399) {
    if (i%1409!=0 || i==1409) {
    if (i%1423!=0 || i==1423) {
    if (i%1427!=0 || i==1427) {
    if (i%1429!=0 || i==1429) {
    if (i%1433!=0 || i==1433) {
    if (i%1439!=0 || i==1439) {
    if (i%1447!=0 || i==1447) {
    if (i%1451!=0 || i==1451) {
    if (i%1453!=0 || i==1453) {
    if (i%1459!=0 || i==1459) {
    if (i%1471!=0 || i==1471) {
    if (i%1481!=0 || i==1481) {
    if (i%1483!=0 || i==1483) {
    if (i%1487!=0 || i==1487) {
    if (i%1489!=0 || i==1489) {
    if (i%1493!=0 || i==1493) {
    if (i%1499!=0 || i==1499) {
    if (i%1511!=0 || i==1511) {
    if (i%677!=0 || i==677) {
    if (i%683!=0 || i==683) {
    if (i%691!=0 || i==691) {
    if (i%701!=0 || i==701) {
    if (i%3!=0 || i==3) {
    if (i%5!=0 || i==5) {
    if (i%7!=0 || i==7) {
    if (i%11!=0 || i==11) {
    if (i%13!=0 || i==13) {
    if (i%17!=0 || i==17) {
    if (i%19!=0 || i==19) {
    if (i%23!=0 || i==23) {
    if (i%29!=0 || i==29) {
    if (i%31!=0 || i==31) {
    if (i%37!=0 || i==37) {
    if (i%41!=0 || i==41) {
    if (i%43!=0 || i==43) {
    if (i%47!=0 || i==47) {
    if (i%53!=0 || i==53) {
    if (i%59!=0 || i==59) {
    if (i%61!=0 || i==61) {
    if (i%67!=0 || i==67) {
    if (i%71!=0 || i==71) {
    if (i%73!=0 || i==73) {
    if (i%79!=0 || i==79) {
    if (i%83!=0 || i==83) {
    if (i%89!=0 || i==89) {
    if (i%97!=0 || i==97) {
    if (i%101!=0 || i==101) {
    if (i%103!=0 || i==103) {
    if (i%107!=0 || i==107) {
    if (i%109!=0 || i==109) {
    if (i%113!=0 || i==113) {
    if (i%127!=0 || i==127) {
    if (i%131!=0 || i==131) {
    if (i%137!=0 || i==137) {
    if (i%139!=0 || i==139) {
    if (i%149!=0 || i==149) {
    if (i%151!=0 || i==151) {
    if (i%157!=0 || i==157) {
    if (i%163!=0 || i==163) {
    if (i%167!=0 || i==167) {
    if (i%173!=0 || i==173) {
    if (i%179!=0 || i==179) {
    if (i%181!=0 || i==181) {
    if (i%191!=0 || i==191) {
    if (i%193!=0 || i==193) {
    if (i%197!=0 || i==197) {
    if (i%199!=0 || i==199) {
    if (i%211!=0 || i==211) {
    if (i%223!=0 || i==223) {
    if (i%227!=0 || i==227) {
    if (i%229!=0 || i==229) {
    if (i%233!=0 || i==233) {
    if (i%239!=0 || i==239) {
    if (i%241!=0 || i==241) {
    if (i%251!=0 || i==251) {
    if (i%257!=0 || i==257) {
    if (i%263!=0 || i==263) {
    if (i%269!=0 || i==269) {
    if (i%271!=0 || i==271) {
    if (i%277!=0 || i==277) {
    if (i%281!=0 || i==281) {
    if (i%283!=0 || i==283) {
    if (i%293!=0 || i==293) {
    if (i%307!=0 || i==307) {
    if (i%311!=0 || i==311) {
    if (i%313!=0 || i==313) {
    if (i%317!=0 || i==317) {
    if (i%331!=0 || i==331) {
    if (i%337!=0 || i==337) {
    if (i%347!=0 || i==347) {
    if (i%349!=0 || i==349) {
    if (i%353!=0 || i==353) {
    if (i%359!=0 || i==359) {
    if (i%367!=0 || i==367) {
    if (i%373!=0 || i==373) {
    if (i%379!=0 || i==379) {
    if (i%383!=0 || i==383) {
    if (i%389!=0 || i==389) {
    if (i%397!=0 || i==397) {
    if (i%401!=0 || i==401) {
    if (i%409!=0 || i==409) {
    if (i%419!=0 || i==419) {
    if (i%421!=0 || i==421) {
    if (i%431!=0 || i==431) {
    if (i%433!=0 || i==433) {
    if (i%439!=0 || i==439) {
    if (i%443!=0 || i==443) {
    if (i%449!=0 || i==449) {
    if (i%457!=0 || i==457) {
    if (i%461!=0 || i==461) {
    if (i%463!=0 || i==463) {
    if (i%467!=0 || i==467) {
    if (i%479!=0 || i==479) {
    if (i%487!=0 || i==487) {
    if (i%491!=0 || i==491) {
    if (i%499!=0 || i==499) {
    if (i%503!=0 || i==503) {
    if (i%509!=0 || i==509) {
    if (i%521!=0 || i==521) {
    if (i%523!=0 || i==523) {
    if (i%541!=0 || i==541) {
    if (i%547!=0 || i==547) {
    if (i%557!=0 || i==557) {
    if (i%563!=0 || i==563) {
    if (i%569!=0 || i==569) {
    if (i%571!=0 || i==571) {
    if (i%577!=0 || i==577) {
    if (i%587!=0 || i==587) {
    if (i%593!=0 || i==593) {
    if (i%599!=0 || i==599) {
    if (i%601!=0 || i==601) {
    if (i%607!=0 || i==607) {
    if (i%613!=0 || i==613) {
    if (i%617!=0 || i==617) {
    if (i%619!=0 || i==619) {
    if (i%631!=0 || i==631) {
    if (i%641!=0 || i==641) {
    if (i%643!=0 || i==643) {
    if (i%647!=0 || i==647) {
    if (i%653!=0 || i==653) {
    if (i%659!=0 || i==659) {
    if (i%661!=0 || i==661) {
    if (i%673!=0 || i==673) {
    if (i%677!=0 || i==677) {
    if (i%683!=0 || i==683) {
    if (i%691!=0 || i==691) {
    if (i%701!=0 || i==701) {
    if (i%709!=0 || i==709) {
    if (i%719!=0 || i==719) {
    if (i%727!=0 || i==727) {
    if (i%733!=0 || i==733) {
    if (i%739!=0 || i==739) {
    if (i%743!=0 || i==743) {
    if (i%751!=0 || i==751) {
    if (i%757!=0 || i==757) {
    if (i%761!=0 || i==761) {
    if (i%769!=0 || i==769) {
    if (i%773!=0 || i==773) {
    if (i%787!=0 || i==787) {
    if (i%797!=0 || i==797) {
    if (i%809!=0 || i==809) {
    if (i%811!=0 || i==811) {
    if (i%821!=0 || i==821) {
    if (i%823!=0 || i==823) {
    if (i%827!=0 || i==827) {
    if (i%829!=0 || i==829) {
    if (i%839!=0 || i==839) {
    if (i%853!=0 || i==853) {
    if (i%857!=0 || i==857) {
    if (i%859!=0 || i==859) {
    if (i%863!=0 || i==863) {
    if (i%877!=0 || i==877) {
    if (i%881!=0 || i==881) {
    if (i%883!=0 || i==883) {
    if (i%887!=0 || i==887) {
    if (i%907!=0 || i==907) {
    if (i%911!=0 || i==911) {
    if (i%919!=0 || i==919) {
    if (i%929!=0 || i==929) {
    if (i%937!=0 || i==937) {
    if (i%941!=0 || i==941) {
    if (i%947!=0 || i==947) {
    if (i%953!=0 || i==953) {
    if (i%967!=0 || i==967) {
    if (i%971!=0 || i==971) {
    if (i%977!=0 || i==977) {
    if (i%991!=0 || i==991) {
    if (i%997!=0 || i==997) {
    if (i%1009!=0 || i==1009) {
    fprintf(fp, "%d,",i);
    prime=prime+1;
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    else
    {
    fp=fopen("primes1.txt","w");
    }
    }
    printf("%d\n",prime);
    system("pause");
    	return 0;
    }
    //---------------------------------------------------------------------------
    
    //---------------------------------------------------------------------------
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You're kidding us right? Nobody in his/her right mind writes code like that. Have a look at this:

    Code:
    #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;
    
    	for (i= 2; i < 10000; i++)
    		if (isPrime(i))
    			printf("%d\n");
    
    	return 0;
    }
    kind regards,

    Jos

    Comment

    • scienceman88
      New Member
      • Aug 2009
      • 85

      #3
      don't even understand that maybe that's why I never made it like that

      Comment

      • scienceman88
        New Member
        • Aug 2009
        • 85

        #4
        Also Jos I'm looking for a loop to shorten it not a sidenote of how crazy i am

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by scienceman88
          Also Jos I'm looking for a loop to shorten it not a sidenote of how crazy i am
          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.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            So you wouldn't call Jos loop rather shorter than your code then?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              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

              Code:
              Populate LIST will all integers from 2 to 18 Quintillion
              
              LOOP
                  Get next NUMBER in list
                  Remove all multiples of NUMBER except NUMBER*1 from the list
              UNTIL END IS REACHED
              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.

              Comment

              • hsheboul
                New Member
                • May 2009
                • 14

                #8
                Primality Testing

                To test for a number whether it's a prime or not is "undecidabl e," 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 "Optimizati on Technique being used/adopted."

                Code:
                /* define your list of sentinels, this BASIS list might change according to the OT. */
                
                int sentinels[] =  {2, 3}  /* According to OT, 0 and 1 might be added */
                int k = 2; /* the next avail slot in sentinels */
                /* sentinels will be re-enlarged, re-allocate space using ralloc(), but less efficient to be re-allocated in the below function  */
                function is_prime(integral_type n)
                {
                  /* k might be declared as static instead of being global above */
                  if (n in sentinels)
                    return TRUE;
                  /* compute the square root of n */
                  i = 3; /* RO */
                  r = floor(square_root(n)); /* RO */
                  while (i <= r && n % i) i++; /* Change of OT, then RO */
                  if ( i < r) /* premature */
                    return FALSE;
                  sentinels[k++] = n;
                  return TRUE;
                }
                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 "predetermi ned" 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.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by scienceman88
                  don't even understand that [Message #2] maybe that's why I never made it like that
                  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.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by hsheboul
                    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.
                    Code:
                    int sentinels[] =  {2, 3}  /* According to OT, 0 and 1 might be added */
                    int k = 2; /* the next avail slot in sentinels */
                    ...
                      sentinels[k++] = n;
                    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):
                    Code:
                    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.

                    Comment

                    • hsheboul
                      New Member
                      • May 2009
                      • 14

                      #11
                      Originally posted by donbock
                      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):
                      Code:
                      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.
                      You are right Don, I was not that precise in writing the pseudo code.

                      Originally posted by donbock
                      if you know ahead of time that you intend to search for all primes less than N,
                      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
                      The oldest and best Internet source for information on record primes! Update daily. Do you want to know the largest prime and who found it? How about the largest twin prime? Or the largest Sophie Germain? Then check out this page. We have prime records, resources and references. Includes a searchable automated database of the 5000 largest known primes.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by scienceman88
                        Also Jos I'm looking for a loop to shorten it not a sidenote of how crazy i am
                        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

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by JosAH
                          i.e. if a number evenly divides by 6*x +- 1 it can never be a prime number.
                          [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

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by Banfa
                            [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
                            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 ;-)

                            Comment

                            • scienceman88
                              New Member
                              • Aug 2009
                              • 85

                              #15
                              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

                              Comment

                              Working...