![]() |
|
|
#2058 | |
|
Aug 2006
135338 Posts |
Quote:
for(n=1, _, ... prime(n) ...) you should probably be writing forprime(p=2, prime(_), ... p ...) instead. It's significantly faster. (Actually, in this case it isn't because the powerings take a long time, but in general this can make a big difference.) Also, since the numbers get so large so quickly, I recommend working mod the appropriate power of ten rather than storing the whole number and reducing. So Code:
addhelp(SumPPowerPModM, "SumPPowerP(lim,m): Returns the sum of p^p, p prime and <= lim, mod m.");
SumPPowerPModM(lim, m)={
my(SumPPowerP=Mod(0,m));
forprime(p=2,lim,SumPPowerP+=Mod(p,m)^p);
lift(SumPPowerP)
};
|
|
|
|
|
|
|
#2059 |
|
Mar 2006
Germany
22·727 Posts |
Ok, thanks for the advice.
The function SumPPowerP(m) was meant to be as it is. I would create a new function like you did then for the MOD-values. And further more it's possible, to use file reading and writing for higher m-values to calculate the next power from a saved file. I've done this before your post, so a combination of file-read/write and MOD should be best. Here's what I've done for my original functions: - Create a folder named "PPowerP" (in PARI-installation folder) to hold those save-files To create a new file for PPowerP(m) use: Code:
addhelp(WritePPowerPFile, "WritePPowerPFile(m): Write PPowerP(m) into a file.");
WritePPowerPFile(m)={
write("PPowerP/PPowerP"m".txt",SumPPowerP(m));
};
To create from this file the next say for m=2000 use Code:
addhelp(WritePPowerPFile, "WritePPowerPFile(m): Write PPowerP(n) to file with template from PPowerP(m)-file.");
WritePPowerPFromFile(m,n)={
my(PP=read("PPowerP/PPowerP"m".txt"));
for(x=m+1,n,PP+=prime(x)^prime(x));
write("PPowerP/PPowerP"n".txt",PP);
};
I've not yet inserted the 'forprime' usage and the MOD but this should not such big problem here. Perhaps a trap should be used to exclude errors when a file is not found. |
|
|
|
|
|
#2060 |
|
Jun 2003
5,087 Posts |
|
|
|
|
|
|
#2061 | |
|
Aug 2006
3·1,993 Posts |
Quote:
In short: you were (essentially) right, I was wrong.
|
|
|
|
|
|
|
#2062 |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Code:
x=vector(100,n,if(isprime(n),n));x=vecsort(x);for(y=1,#x-1,if(y<=#x && x[y]==0,x=vector(#x-1,n,x[n+1]);x=concat(x,0)));for(b=1,#x,if(b=<#x && x[b]==0,x=vector((b-1),n,x[n]);break())) |
|
|
|
|
|
#2063 |
|
Aug 2006
3×1,993 Posts |
I get a different error message:
Code:
*** syntax error, unexpected '<': for(b=1,#x,if(b=<#x&&x[b]==0,x=vecto Last fiddled with by CRGreathouse on 2010-12-14 at 20:16 |
|
|
|
|
|
#2064 | |
|
"Forget I exist"
Jul 2009
Dumbassville
838410 Posts |
Quote:
but now it seems to only works for random lengths that I have no patern to lol 1068 works as a limit 1000 doesn't 100 worked but 108 or 109 wouldn't. Last fiddled with by science_man_88 on 2010-12-14 at 21:22 |
|
|
|
|
|
|
#2065 |
|
"Forget I exist"
Jul 2009
Dumbassville
100000110000002 Posts |
I named my code to show you:
Code:
(17:28)>for(x=1,100,print(primesUPTO(x)","x)) [],1 [2],2 [2, 3],3 [],4 [],5 [2, 3, 5],6 [2, 3, 5, 7],7 [],8 [2, 3, 5, 7],9 [],10 [],11 [2, 3, 5, 7, 11],12 [2, 3, 5, 7, 11, 13],13 [],14 [2, 3, 5, 7, 11, 13],15 [],16 [],17 [2, 3, 5, 7, 11, 13, 17],18 [2, 3, 5, 7, 11, 13, 17, 19],19 [],20 [2, 3, 5, 7, 11, 13, 17, 19],21 [],22 [],23 [2, 3, 5, 7, 11, 13, 17, 19, 23],24 [],25 [2, 3, 5, 7, 11, 13, 17, 19, 23],26 [],27 [2, 3, 5, 7, 11, 13, 17, 19, 23],28 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29],29 [],30 [],31 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31],32 [],33 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31],34 [],35 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31],36 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37],37 [],38 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37],39 [],40 [],41 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41],42 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43],43 [],44 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43],45 [],46 [],47 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47],48 [],49 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47],50 [],51 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47],52 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53],53 [],54 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53],55 [],56 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53],57 [],58 [],59 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59],60 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61],61 [],62 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61],63 [],64 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61],65 [],66 [],67 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67],68 [],69 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67],70 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71],71 [],72 [],73 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73],74 [],75 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73],76 [],77 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73],78 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79],79 [],80 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79],81 [],82 [],83 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83],84 [],85 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83],86 [],87 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83],88 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89],89 [],90 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89],91 [],92 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89],93 [],94 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89],95 [],96 [],97 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],98 [],99 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],100 Last fiddled with by science_man_88 on 2010-12-14 at 21:30 |
|
|
|
|
|
#2066 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Code:
sieve_of_eratosthenes(x) = a=0;c=[2,3];for(y=4,x,for(z=1,#c,if(y%c[z]==0,break(),a=a+1));if(a==#c,c=concat(c,y));a=0);c Last fiddled with by science_man_88 on 2010-12-14 at 22:13 |
|
|
|
|
|
#2067 |
|
Jun 2003
5,087 Posts |
This is decidedly _not_ Sieve of Eratosthenes. This is called "trial division". And a slow version at that. You'll get good reference pseudocode for SoE on the web. Try to implement one of those.
Last fiddled with by axn on 2010-12-14 at 22:22 Reason: Erat... |
|
|
|
|
|
#2068 |
|
Mar 2006
Germany
22·727 Posts |
Like this:
Code:
SoE(n)={
my(v=vector(n,m,1));
v[1]=0;
forstep(i=4,n,2,v[i]=0);
for(i=3,n\2,
if(v[i]==1,
forstep(j=2*i,n,i,v[j]=0)));
v
};
PrintSoE(n)={
my(v=SoE(n),j=0);
for(i=1,n,if(v[i],print1(i" ");j++));
print;
print(j" primes from 1 to "n);
};
Last fiddled with by kar_bon on 2010-12-14 at 23:10 |
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why do I sometimes see all the <> formatting commands when I quote or edit? | cheesehead | Forum Feedback | 3 | 2013-05-25 12:56 |
| Passing commands to PARI on Windows | James Heinrich | Software | 2 | 2012-05-13 19:19 |
| Ubiquity commands | Mini-Geek | Aliquot Sequences | 1 | 2009-09-22 19:33 |
| 64-bit Pari? | CRGreathouse | Software | 2 | 2009-03-13 04:22 |
| Are these commands correct? | jasong | Linux | 2 | 2007-10-18 23:40 |