![]() |
[QUOTE=CRGreathouse;265082]Yes, I had to interrupt the code to see what it was doing as it had fallen into an infinite loop. If your third version works, good for you.[/QUOTE]
no my first version worked better than my last one the loop isn't infinite it just has to recalculate a[#a] twice on the fly. that's basically the major difference I see. |
[QUOTE=science_man_88;265083]no my first version worked better than my last one the loop isn't infinite it just has to recalculate a[#a] twice on the fly. that's basically the major difference I see.[/QUOTE]
The version that you posted in post #2252 is an infinite loop, since each prime it adds to a is 3. For example, if I add [code]print1(b[z]" ");[/code] just before [code]if(b[z]!=0,a=concat(a,b[z]);break)[/code] I get [code]3 3 3 3 3 3 3 3 [3000 more 3s skipped] 3 3 3 3 3 3 3 ^C *** at top-level: Erastosthenes1(100) *** ^------------------- *** in function Erastosthenes1: ...int1(b[z]" ");if(b[z]!=0, *** a=concat(a,b[z]);bre *** ^-------------------- *** user interrupt after 330 ms. *** Break loop: type <Return> to continue; 'break' to go back to GP[/code] |
My browser's having a hard time rendering that uber-long string of 3's. Is it really necessary?
|
[QUOTE=CRGreathouse;265085]The version that you posted in post #2252 is an infinite loop, since each prime it adds to a is 3. For example, if I add
[code]print1(b[z]" ");[/code] just before [code]if(b[z]!=0,a=concat(a,b[z]);break)[/code] I get [code]3 3 3 3 3 3 3 3 3 (skipped 3's ) 3 3 3 ^C *** at top-level: Erastosthenes1(100) *** ^------------------- *** in function Erastosthenes1: ...int1(b[z]" ");if(b[z]!=0, *** a=concat(a,b[z]);bre *** ^-------------------- *** user interrupt after 330 ms. *** Break loop: type <Return> to continue; 'break' to go back to GP[/code][/QUOTE] I don't get any of that on my end except me stopping it. |
What do you get if you add the print statement I suggested?
|
[QUOTE=bsquared;265086]My browser's having a hard time rendering that uber-long string of 3's. Is it really necessary?[/QUOTE]
After my computer lovingly crafted each and every one of those 3s? I'm shocked by your audacity. |
[QUOTE=CRGreathouse;265089]After my computer lovingly crafted each and every one of those 3s? I'm shocked by your audacity.[/QUOTE]
They were a beautiful thing... I'm almost sorry to see them go. :max:But now they're back thanks to sm88's quote. :max: |
[QUOTE=CRGreathouse;265088]What do you get if you add the print statement I suggested?[/QUOTE]
I get a whole bunch of 3's but I don't see how it's possible outside of one thing: for the start of the forstep you need a if statement: [CODE](x)->a=[2];b=vector(x-2,n,n+2);until(a[#a]>sqrt(x),forstep(y=if(a[#a]==2,a[#a],a[#a]-2),#b,a[#a],b[y]=0);for(z=1,#b,if(b[z]!=0,a=concat(a,b[z]);break())));a[/CODE] this happened because I made a have 2 in it to start I think. and because trying to eliminate multiples of three with a n+2 setup in the array made 3 in position 1 not 3 so it never gets eliminated. |
[QUOTE=science_man_88;265091]I get a whole bunch of 3's but I don't see how it's possible outside of one thing:
for the start of the forstep you need a if statement: [CODE](x)->a=[2];b=vector(x-2,n,n+2);until(a[#a]>sqrt(x),forstep(y=if(a[#a]==2,a[#a],a[#a]-2),#b,a[#a],b[y]=0);for(z=1,#b,if(b[z]!=0,a=concat(a,b[z]);break())));a[/CODE] this happened because I made a have 2 in it to start I think. and because trying to eliminate multiples of three with a n+2 setup in the array made 3 in position 1 not 3 so it never gets eliminated.[/QUOTE] now it's missing the part to add in the values greater than sqrt(x). |
Let's do this again. Use only one array and do something like this:
[code]b=vector(x,unused,1); b[1]=0; ptr=2; while(b[ptr] <= sqrt(x), \\ cross off numbers divisible by b[ptr] ptr++; while(b[ptr] == 0, ptr++); ); \\ Now b[n] is 1 if n is prime and 0 otherwise \\ Output the primes numberOfPrimes=sum(i=1,#b,b[i]); n=0; vector(numberOfPrimes,unused, while(b[n]==0,n++); n )[/code] |
[QUOTE=CRGreathouse;265096]Let's do this again. Use only one array and do something like this:
[code]b=vector(x,unused,1); b[1]=0; ptr=2; while(b[ptr] <= sqrt(x), \\ cross off numbers divisible by b[ptr] ptr++; while(b[ptr] == 0, ptr++); ); \\ Now b[n] is 1 if n is prime and 0 otherwise \\ Output the primes numberOfPrimes=sum(i=1,#b,b[i]); n=0; vector(numberOfPrimes,unused, while(b[n]==0,n++); n )[/code][/QUOTE] [CODE], ptr++););numberOfPrimes=sum(i=1,#b,b[i]);n=0;vector (x)->b=vector(x,unused,1);b[1]=0;ptr=2;while(b[ptr]<= >eratosene4(100) hile: array index (101) out of allowed range [1-100].[/CODE] |
| All times are UTC. The time now is 22:58. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.