![نقل قول](images/misc/quote_icon.png)
نوشته شده توسط
FastCode
این هم یدونه از طرف من.
یکی از دوستام گفت که یه برنامه بده ک اعداد اول رو حساب کنه.
باید یه چیزی مینوشتم که توی sms جا بشه.
نخندید.
#include "stdio.h"
#include "malloc.h"
#define U unsigned int
int main(){U S,E,m=1,c=1,V;printf("S? E?");scanf("%u %u",&S,&E);if(S>E)return 1;if((E>1)&&(S<2))printf("%u\n",2);U* L=(U*)malloc(6542*sizeof(U));L[0]=2;bool D=0;for(U n=2;n!=E;n++){for(U t=0;t!=c;t++)if((n%(V=L[t]))==0)goto x;if((V*V<n)&&((n%(V=L[C++]))==0))goto x;if(n>S)printf("%u\n",n);if(!D){L[m++]=n;if(n*n>E){D=1;if(n<S){while((V*V)<S)V=L[C++];n=S;}}}x:;}free(L);return 0;}
با gcc روی دبییان x86_64, armel و vC++ 6 روی ویندوز x86 کامپایل میشه.
#include "stdio.h"
#include "malloc.h"
int main(){
unsigned int Start, End, CurrentMemo = 1, CurrentTest = 1, SquareRoot;
printf("S? E?");
scanf("%u %u", &Start, &End);
if(Start > End)
return 1;
if((End > 1) && (Start < 2))
printf("%u\n", 2);
unsigned int* List = (unsigned int*)malloc(6542 * sizeof(unsigned int));
List[0] = 2;//CurrentMemo is Zero, we can use it here and change the initial value instead
bool DoneWithMemo = 0;
for(unsigned int Counter = 2;Counter != End;Counter++) {
for(unsigned int TestCounter = 0;TestCounter != Counter;TestCounter++)
if((Counter % (SquareRoot = List[TestCounter])) == 0)
goto Next;
if((SquareRoot * SquareRoot < Counter) && ((Counter % (SquareRoot = List[CurrentTest++])) == 0))
goto Next;
if(Counter > Start)//It`s prime
printf("%u\n", n);
if(!DoneWithMemo) {
List[CurrentMemo++] = Counter;//we need this prime
if(Counter * Counter > End) {
DoneWithMemo = 1;
if(Counter < Start) {
while((SquareRoot * SquareRoot) < Start) //find the least needed SquareRoot
SquareRoot = List[CurrentTest++];
Counter = Start;
}
}
}
Next:;
}
free(L);
return 0;
}
اون موقع میفهمیدم این کد چکار میکنه.
الان هم میفهمم ولی با اینکه مرتبش کردم نمیدونم چطوری