![]() |
[QUOTE=science_man_88;280132]thanks I already proved myself wrong without it but I still want to see if it even semi fits:
[CODE](17:34)>b=2;forprime(p=2281,2281,for(y=1,p-1,b=sqrt(Mod(b,1<<p-1));trap(,break(),);if(y==p-1,print(p)));b=2) *** sqrt: user interrupt after 13,750 ms. *** break: bug in PARI/GP (Segmentation Fault), please report *** Break loop (type 'break' or Control-d to go back to GP)[/CODE][/QUOTE] [CODE](17:37)>b=2;forprime(p=29,29,for(y=1,p-1,b=sqrt(Mod(b,1<<p-1));if(y==p-1,print(p)));b=2) *** sqrt: non quadratic residue in gsqrt *** break: bug in PARI/GP (Segmentation Fault), please report *** Break loop (type 'break' or Control-d to go back to GP)[/CODE] |
hopefully I don't have to reinstall ( though likely) I've had segmentation faults too much ( stupidly ignored a little) and even my timer seems wacky at times.
|
You're using trap wrong. You're writing
code; trap(, stuff, ) when I told you to write trap(, code, stuff) What you did will still cause the error. [QUOTE=science_man_88;280136]hopefully I don't have to reinstall[/QUOTE] No, you don't need to do that. |
I don't know what are you trying to do (semantically), but (syntactically) you should write this:
[CODE] (10:06:27) gp > b=2;forprime(p=2,2281,for(y=1,p-1,trap(,print("break here, p="p", y="y);break,b=sqrt(Mod(b,1<<p-1)));if(y==p-1,print(p)));b=2) break here, p=2, y=1 break here, p=3, y=2 break here, p=5, y=3 7 break here, p=11, y=6 break here, p=13, y=7 break here, p=17, y=5 break here, p=19, y=10 23 break here, p=29, y=15 31 break here, p=37, y=19 break here, p=41, y=11 break here, p=43, y=8 47 break here, p=53, y=27 [/CODE] |
It's so much nicer if you format the code!
[code]{b=2; forprime(p=2,2281, for(y=1,p-1, trap(, print("break here, p="p", y="y); break , b=sqrt(Mod(b,1<<p-1)) ); if(y==p-1,print(p)) ); b=2 )}[/code] I can't figure out the intent either. |
after coming across a comment at:
[url]https://oeis.org/A002450[/url] I did some searching about the topic and came across a reference to Sierpinski triangle and rule 90 with that I was able to get to this: [CODE](n)-> a=[[[1,1,1],0],[[1,1,0],1],[[1,0,1],0],[[1,0,0],1],[[0,1,1],1],[[0,1,0],0],[[0,0,1],1],[[0,0,0],0]]; b=matrix(2^(n-1)+2,2^n-1+2); b[2,(#b+1)/2]=1; for(x=3,2^(n-1)-1, for(y=2,#b-3, for(z=1,#a, if(a[z][2]==1, b[x,y+1]=a[z][2] ) ) ) ) ;b[/CODE] as a almost working code to make the Sierpinski triangle Wikipedia helped in my understanding a bit as well as playing around, but my problem is the if statement in the last for loop I want to check the three I need to get the ones and zeroes in the correct placement for what my layout should be ( and yes my layout has a border, no that's actually not what I checked before I just did that as a test case). according to [url]http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_PARI/GP[/url] the triangle has still not been done. |
[CODE](19:29)>Sierpinski(4)
%352 = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 [COLOR="Red"]1[/COLOR] 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 [COLOR="Red"]1 0 1[/COLOR] 0 0 0 0 0 0 0] [0 0 0 0 0 0 [COLOR="Red"]1 0 0 0 1[/COLOR] 0 0 0 0 0 0] [0 0 0 0 0 [COLOR="Red"]1 0 1 0 1 0 1[/COLOR] 0 0 0 0 0] [0 0 0 0 [COLOR="Red"]1 0 0 0 0 0 0 0 1[/COLOR] 0 0 0 0] [0 0 0 [COLOR="Red"]1 0 1 0 0 0 0 0 1 0 1[/COLOR] 0 0 0] [0 0 [COLOR="Red"]1 0 0 0 1 0 0 0 1 0 0 0 1[/COLOR] 0 0] [0 [COLOR="Red"]1 0 1 0 1 0 1 0 1 0 1 0 1 0 1[/COLOR] 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][/CODE] just realized this wasn't it, [CODE](n)-> a=[[[1,1,1],0],[[1,1,0],1],[[1,0,1],0],[[1,0,0],1],[[0,1,1],1],[[0,1,0],0],[[0,0,1],1],[[0,0,0],0]]; b=matrix(2^(n-1)+2,2^n-1+2); b[2,(#b+1)/2]=1; for(x=3,2^(n-1)+1, for(y=1,#b-2, for(z=1,#a, if(eval(Vec(Str(b[x-1,y]b[x-1,y+1]b[x-1,y+2])))==a[z][1], b[x,y+1]=a[z][2] ) ) ) ) ;b[/CODE] never mind it's the way I printed it that got me confused this is it. and it works for all n>=1 I believe. |
[QUOTE=cmd;282102]3 .. Spring roll s in ..
:bear:[/QUOTE] [CODE]x=1000000000;d=#divisors(x);for(y=1,ceil(d/2),print(divisors(x)[y]"\t\t\t "divisors(x)[d-(y-1)]))[/CODE] is my best version of it except I only list everything once. |
working with matrices
[CODE](09:30)>a=matrix(10,2);print(#a[,1]);print(#a);
10 2[/CODE] |
is there an easy way to create all the distinct partitions of a number n without the number 1 involved ( though I also have a way with 1 involved I think) ? because I'm trying to speed up my aligen function and it's based on partitions.
|
[QUOTE=science_man_88;284065]is there an easy way to create all the distinct partitions of a number n without the number 1 involved ( though I also have a way with 1 involved I think) ? because I'm trying to speed up my aligen function and it's based on partitions.[/QUOTE]
looks to be a no there's ways of counting them but I want the partitions themselves because working backwards through aliquot sequences can be based in partitions of n-1 where n is the number we are saying is sigma(x)-x ( my current script does checks) for example: n=6; a=n-1=5; a=5=4+1=3+2 the 5 is by itself so square it, then we without duplicating the 1 we took off we get get 5^2,3*2 , gives you 25 and 6 and only those as 5 can't be partitioned in h>3 parts without a 1 being involved. a sigma check on both shows they both work out . |
[CODE]homeprime(n)= s=n;until(isprime(s),a=factor(s);b="";for(x=1,#a[,2],until(a[x,2]==0,b=concat(b,Str(a[x,1]));a[x,2]=a[x,2]-1));print(b);s=eval(b))[/CODE]
made this after reading in the HP(49) thread. which gave me links to learn about home primes. |
[QUOTE=science_man_88;284065]is there an easy way to create all the distinct partitions of a number n without the number 1 involved ( though I also have a way with 1 involved I think) ? because I'm trying to speed up my aligen function and it's based on partitions.[/QUOTE]
Here's some naive code: [code]partNo1(n)={ select(v->vecmin(v)>1,partitions(n)) }; partNo1Count(n)=polcoeff((1-'x)/eta('x+'x*O('x^n)), n);[/code] If you're running an old version of gp (you probably are), you'll need to change the order of the arguments in select. If you don't like the Vecsmall appearing in the output, apply Vec to each member. Both changes:[code]partNo1(n)={ apply(Vec,select(partitions(n),v->vecmin(v)>1)) };[/code] |
[QUOTE=CRGreathouse;291835]Here's some naive code:
[code]partNo1(n)={ select(v->vecmin(v)>1,partitions(n)) }; partNo1Count(n)=polcoeff((1-'x)/eta('x+'x*O('x^n)), n);[/code] If you're running an old version of gp (you probably are), you'll need to change the order of the arguments in select. If you don't like the Vecsmall appearing in the output, apply Vec to each member. Both changes:[code]partNo1(n)={ apply(Vec,select(partitions(n),v->vecmin(v)>1)) };[/code][/QUOTE] I got back that partitions is not a function. |
[QUOTE=science_man_88;291861]I got back that partitions is not a function.[/QUOTE]
this gets too complicated really quick: duplication through permutation is a problem that can up, so was the avoiding using 1's so are the ones with 2 of the same number. so many things to consider, no wonder a lot of people take the other routes. for my purposes 2,3==3,2 1's are unnecessary because I already subtract it from n, the duplicate part means any partition with x,x in it becomes unwanted. |
[QUOTE=science_man_88;291861]I got back that partitions is not a function.[/QUOTE]
That was added in version 2.4.3, released two years ago. Upgrade your version of gp and get all the latest stuff. |
[QUOTE=CRGreathouse;291926]That was added in version 2.4.3, released two years ago. Upgrade your version of gp and get all the latest stuff.[/QUOTE]
[url]http://pari.math.u-bordeaux.fr/download.html[/url] latest posted for a self installing one is 2.4.2 I have a download of 2.5.1.exe it is there but the download info says very basic. |
[QUOTE=science_man_88;291968][url]http://pari.math.u-bordeaux.fr/download.html[/url] latest posted for a self installing one is 2.4.2
I have a download of 2.5.1.exe it is there but the download info says very basic.[/QUOTE] [CODE]partNo1(n)= a=Vec(select(v->vecmin(v)>1,partitions(n)));for(x=1,#a,if(#vecsort(a[x],,8)!=#a[x],a[x]=0;a[x]=Vec(a[x])));forstep(y=#a,1,-1,if(a[y]==[0],a=vector(y-1,n,a[n])));a[/CODE] is as far as I have what I think is correct for use with aliquot sequences going backwards. |
Now remember to use my() for all variables you use in your functions...
|
[QUOTE=science_man_88;291983][CODE]partNo1(n)= a=Vec(select(v->vecmin(v)>1,partitions(n)));for(x=1,#a,if(#vecsort(a[x],,8)!=#a[x],a[x]=0;a[x]=Vec(a[x])));forstep(y=#a,1,-1,if(a[y]==[0],a=vector(y-1,n,a[n])));a[/CODE]
is as far as I have what I think is correct for use with aliquot sequences going backwards.[/QUOTE] hmmm this for some reason deletes some that could be used like 2,4,8 for 14 (15-1) to show that 16 comes from 15 going back. admittedly that runs me into more problems like how to deal with the fact that 1+2+4+8 = 15 but 2*4*8!=16. apparently I have a ways to go. found the part that deleted 2,4,8 by the looks of it it's the last part. |
[QUOTE=science_man_88;292508]hmmm this for some reason deletes some that could be used like 2,4,8 for 14 (15-1) to show that 16 comes from 15 going back. admittedly that runs me into more problems like how to deal with the fact that 1+2+4+8 = 15 but 2*4*8!=16. apparently I have a ways to go. found the part that deleted 2,4,8 by the looks of it it's the last part.[/QUOTE]
doh I just realized why, 3 and 11 are prime they'd be the only proper divisors of the number but 4 and 8 in the 2,4,8 aren't so there needs to be more numbers added to find the individual values multiplied together 2*8=16 adding 16 to this list 64 would get on the list because it's only using one number, adding 1 to all this leads to a list of proper divisors, doubt it's fast to back check though, so I only need the ones without repeats I've already done that once, then I would need only the first and last elements without adding 1 to it all I believe that should be easy enough. |
okay I think I have this worked out:
[CODE]ali(f)=g=vector(#partNo1(f-1)-1,k,partNo1(f-1)[k+1]);g[/CODE] [CODE]partNo1(n)= a=Vec(select(v->vecmin(v)>1,partitions(n))); b=[]; for(x=1,#a, a[x]=if(#vecsort(a[x],,8)!=#a[x], [0], Vecsmall(Vec(a[x]))); b=concat(b, if((a[x][1]*a[x][#a[x]])!=0 && sigma(a[x][1]*a[x][#a[x]])-(a[x][1]*a[x][#a[x]])==(n+1), a[x][1]*a[x][#a[x]]) ) ); return(vecsort(b,,8))[/CODE] still doesn't work well with large amounts of partitions ( stupid memory limits). |
[QUOTE=science_man_88;291301][CODE]homeprime(n)= s=n;until(isprime(s),a=factor(s);b="";for(x=1,#a[,2],until(a[x,2]==0,b=concat(b,Str(a[x,1]));a[x,2]=a[x,2]-1));print(b);s=eval(b))[/CODE]
made this after reading in the HP(49) thread. which gave me links to learn about home primes.[/QUOTE] I've played with going backwards and the Inverse home prime sequences. going with the inverse you just change to a forstep to concat in the other direction. the backwards home prime gets a little complicated but a few things we can use: home primes concat from lowest to highest so looking at the resulting string/ number working through it from the front end we can check every new prime found against the last to see if it's greater than or equal to it because they need to be at least as big to be in proper order. while reading it from the far end the same is true for IHP. |
[QUOTE=science_man_88;304410]I've played with going backwards and the Inverse home prime sequences. going with the inverse you just change to a forstep to concat in the other direction. the backwards home prime gets a little complicated but a few things we can use: home primes concat from lowest to highest so looking at the resulting string/ number working through it from the front end we can check every new prime found against the last to see if it's greater than or equal to it because they need to be at least as big to be in proper order. while reading it from the far end the same is true for IHP.[/QUOTE]
I talked in PM about this idea to someone and got told to try it on: 189170531644901822763687 manually ( okay typing into pari) I found 18917 was prime then you realized what number starts with 0 normally none so it can't be that the next time it has a end of 1,3,7,or 9 is 18197053 and that works to be prime next thing to realize is that the original numbers next possible prime is supposed to be larger so it too must have at lest 8 digits 16449018 is 8 but doesn't work as it's even next one that it could be is 16449018227 but this leaves less than the length of this new one left so the next one couldn't be bigger a quick check shows that using the rest is prime so we have our 2 primes in this case 18197053 * 1644901822763687 = 31116694961017273454411 ( pari gives a different answer now and this is the answer in the factorDB) |
[B]science_man_88: [/B]it is not a very good idea to publicly engage in meaningful conversations [I]with yourself[/I]. At the very least, you [I]really, really[/I] don't have to quote in full [I]your own message[/I]?! [URL="http://seqanswers.com/forums/faq.php"]Another board[/URL] bluntly classifies such postings as post whoring. (They don't mince words, do they now?)
|
[QUOTE=Batalov;304415][B]science_man_88: [/B]it is not a very good idea to publicly engage in meaningful conversations [I]with yourself[/I]. [/QUOTE]
the fact that anything I say is considered meaningful is somewhat of a surprise especially since it's coming from someone who's not just a basic member. |
It was sarcasm, man. I actually made a bet that it will go undetected.
Sarcasm is when adjectives are deliberately inverted. Example: "That movie? I simply [I]loved*[/I] every bit of it. [URL="http://tvtropes.org/pmwiki/pmwiki.php/Main/SoBadItsGood"]It is so bad that it's actually hilarious[/URL]!" ____ *sarcasm detected. In plain speak, a person would say "hated it" |
[QUOTE=Batalov;304421]It was sarcasm, man. I actually made a bet that it will go undetected.
Sarcasm is when adjectives are deliberately inverted. Example: "That movie? I simply [I]loved*[/I] every bit of it. [URL="http://tvtropes.org/pmwiki/pmwiki.php/Main/SoBadItsGood"]It is so bad that it's actually hilarious[/URL]!" ____ *sarcasm detected. In plain speak, a person would say "hated it"[/QUOTE] maybe it went undetected because if I'm serious about things, I'm not involving sarcasm. |
I can see why it might be hard to do though:
ex. 33191 [CODE]? isprime(3) %25 = 1 ? isprime(331) %26 = 1 ? isprime(3319) %27 = 1 ? isprime(33191) %28 = 1 ? isprime(31) %29 = 1 ? isprime(319) %30 = 0 ? isprime(3191) %31 = 1 ? isprime(19) %32 = 1 ? isprime(191) %33 = 1[/CODE] 8 primes in 5 digits so just looking for primes without looking for is this greater/less than this becomes practically impossible, even looking for greater/less than might be somewhat impossible because it could be 191 or 3191 for the high limit depending on how you break it down. so you can by eye come up with 3*3*191 or 3*3191 for it but using a computer program will get rather complex. |
[code]dp(n)={
if(n<12, return(if(isprime(n), [n], []))); my(v=vecsort(select(isprime, eval(Vec(Str(n)))), , 8), t); while(n>9, if(gcd(n%10, 10)>1, n\=10; next); t=10; while((t*=10)<n*10, if(isprime(n%t), v=concat(v, n%t)) ); v=vecsort(v, , 8); n\=10 ); v }; addhelp(dp, "dp(n): Distinct primes formed from substrings of n. Related to Sloane's A039997 and A093301.");[/code] [code]> dp(33191) %1 = [3, 19, 31, 191, 331, 3191, 3319, 33191] > #% %2 = 8[/code] |
[QUOTE=CRGreathouse;304465][code]dp(n)={
if(n<12, return(if(isprime(n), [n], []))); my(v=vecsort(select(isprime, eval(Vec(Str(n)))), , 8), t); while(n>9, if(gcd(n%10, 10)>1, n\=10; next); t=10; while((t*=10)<n*10, if(isprime(n%t), v=concat(v, n%t)) ); v=vecsort(v, , 8); n\=10 ); v }; addhelp(dp, "dp(n): Distinct primes formed from substrings of n. Related to Sloane's A039997 and A093301.");[/code] [code]> dp(33191) %1 = [3, 19, 31, 191, 331, 3191, 3319, 33191] > #% %2 = 8[/code][/QUOTE] now I'm going to have to figure out which come together to form the numbers . |
I'll have to refocus on the backwards home prime stuff again but for some reason my brain is stuck in do things backwards mode:
[CODE]reverse_collatz(n)=if(n%3==1&&((n-1)/3)%2==1,return([(n-1)/3,2*n]),return([2*n]))[/CODE] |
[code]reverse_collatz(n)=if(n%6==4,[n\3,2*n],[2*n])[/code]
|
[QUOTE=CRGreathouse;304832]Why are you returning one-element vectors?[/QUOTE]
I clearly didn't think deep enough, maybe it would help in recall if it's mixed in with 2 element ones to check for repeating because 1,4,2,1 does exist even if you make it look for n%2==1 values. edit: I messed up everything after 1,4,2,1 I think |
[QUOTE=CRGreathouse;304832][code]reverse_collatz(n)=if(n%6==4,[n\3,2*n],[2*n])[/code][/QUOTE]
I just looked into it using modular arithmetic how can it work because I get: [QUOTE]0->0 1->2,4,0 2->4 3->0 4->1,2,3,5 5->4[/QUOTE] going backwards if 4 mod 6 is the only way to get 1 3 and 5 mod 6 then the Collatz conjecture can't be true over all the positive numbers because the number of numbers that are 1 3 and 5 mod 6 have a total greater than the amount of 4 mod 6 . |
If n is 1 mod 3 and (n-1)/3 is 2k+1 for some k then n-1 is 6k+3 and so n =6k+4 for some k, i.e., is 4 mod 6.
|
[QUOTE=CRGreathouse;304847]If n is 1 mod 3 and (n-1)/3 is 2k+1 for some k then n-1 is 6k+3 and so n =6k+4 for some k, i.e., is 4 mod 6.[/QUOTE]
I get some of that and I see my error it's treating infinity as a limit and treating it like any positive number because there's no limit on the size of number to go through it can continue to stay true even with the 4 mod 6 outnumbered. |
newest idea for the BHP script:
[CODE]BHP(n) =until(n==0,print(dp(n));n=n%(10^(length(Str(n))-1)))[/CODE] admittedly this prints right now it can concat later into a position in a new vector now the problem is keeping only the ones that specifically start with the correct position. of course it may get tricky going to other bases, except for maybe using convert. |
the closest I could get to what's needed for BHP to be like a human searching is:
[CODE]n=33191; a=Vec(Str(n)); b=""; c=vector(#a,z,[]); for(x=1,#a, for(y=x,#a, b=concat(b,a[y]); if(isprime(eval(b)), c[x]=concat(c[x],eval(b)) ) ); b="") ;c[/CODE] Yes I know some are already yelling to use my and I might later, there was a way with duplicates everywhere to get something like this using one vector call but it would take a while to eliminate duplicates from what I could see. with this layout is the difficulty is in finding ways that work to the total length using values in different elements of the outer vector. |
[QUOTE=science_man_88;305180]Yes I know some are already yelling to use my and I might later, there was a way with duplicates everywhere to get something like this using one vector call but it would take a while to eliminate duplicates from what I could see.[/QUOTE]
If a tree falls in a forest and no one is around to hear it, does it make a sound? |
[CODE]y=1;
a=vector(2000000,n, until(isprime(y) && !(y>3 && y%4==3 && isprime(2*y+1)), y=y+1); y )[/CODE] this makes a list of prime exponents to check with LL. admittedly I don't know what other conditions to delete them by, I know what it's searching right now is already done. remade with [CODE]y=y+1[/CODE] into: [CODE]if(y%6==1,y=y+4,y=y+2)[/CODE] |
You should use the select command.
Start with a list of primes [code]candidates=primes(1000);[/code] Remove primes p with 2p+1 prime unless p is 1 mod 4: [code]candidates=select(p -> p%4==1 || !isprime(2*p+1), candidates);[/code] If you're using an older version of gp you need to switch the arguments around: [code]candidates=select(candidates, p -> p%4==3 && isprime(2*p+1));[/code] |
[QUOTE=CRGreathouse;310801]You should use the select command.
Start with a list of primes [code]candidates=primes(1000);[/code] Remove primes p with 2p+1 prime unless p is 1 mod 4: [code]candidates=select(p -> p%4==1 || !isprime(2*p+1), candidates);[/code] If you're using an older version of gp you need to switch the arguments around: [code]candidates=select(candidates, p -> p%4==3 && isprime(2*p+1));[/code][/QUOTE] see the thing I like about my code ( though I admit yours is a lot faster) is that it can start at any number ( in any range) the way it was originally stated and can go faster with the optimizations to it I made. doh just figured out the easy way to do that with your script. |
[QUOTE=science_man_88;310794]... into: [CODE]if(y%6==1,y=y+4,y=y+2)[/CODE][/QUOTE]
hi, try : [CODE] y+=if(y%6==1,4,2) [/CODE] |
this came out of someone talking annual capital gains percentage but me interpreting it as interest rate annually:
[CODE]gainsversusinterest(a,b,c,d)= (((a/b)/c)*d)^(1/d)[/CODE] a is the money unit/valued unit between b and the current value, b is the \$value at a given past time, c is the number of time units to gain those \$a, d is the number time units to forecast at this rate into the future, the ^(1/d) is taking the interest rate equivalent compound. |
an attempted remake ( and correction at last check) of the FaR script:
[CODE] FaR(S1,S2,S3) = aa=eval(Vec(S1));for(x=1,#aa,if(vecextract(aa,)==eval(Vec(S2)),)) [/CODE] the problem is though I know a range string works I don't see how to put it into it based on the variables x ,and length of S2, also the actual replacing become hard this way I think, anyone with an idea for the solutions? |
[QUOTE=science_man_88;343116]the problem is though I know a range string works I don't see how to put it into it based on the variables x ,and length of S2, also the actual replacing become hard this way I think, anyone with an idea for the solutions?[/QUOTE]
If you can be very specific in explaining what you want, I may be able to help. |
[QUOTE=CRGreathouse;343117]If you can be very specific in explaining what you want, I may be able to help.[/QUOTE]
[quote]? ?vecextract vecextract(x,y,{z}): extraction of the components of the matrix or vector x according to y and z. If z is omitted, y represents columns, otherwise y corresponds to rows and z to columns. y and z can be vectors (of indices), strings (indicating ranges as in "1..10") or masks (integers whose binary representation indicates the indices to extract, from left to right 1, 2, 4, 8, etc.).[/quote] This talks of the string range option. I'm trying to use it with a variable x as the starting position and only go as far as the length of the S2 variable further. This is so I can the whole length vector version of S2 to something the same length (instead of looping through each letter), if it gets found then my effort goes to working on the problems of replacing. edit: I realized that the vector option was an option but then I still fail to make it work. |
[QUOTE=science_man_88;343118]This talks of the string range option. I'm trying to use it with a variable x as the starting position and only go as far as the length of the S2 variable further. This is so I can the whole length vector version of S2 to something the same length (instead of looping through each letter), if it gets found then my effort goes to working on the problems of replacing.
edit: I realized that the vector option was an option but then I still fail to make it work.[/QUOTE] I look to have it working now, as to the replace this might be fun if S2 and S3 aren't the same length. edit: [CODE]FaR(S1,S2,S3)=aa=eval(Vec(S1));for(x=1,#aa-length(S2)+1,if(vecextract(aa,vector(length(S2),n,x+n-1))==eval(Vec(S2)),print1(S3);x=x+length(S2)-1,if(x==(#aa-length(S2)+1),for(y=x,#aa,print1(aa[x])), print1(aa[x]))))[/CODE] is my best for right now, it still prints 0 when it doesn't match it seems. |
Found a bug in pari's interpreter. I thought I was going mad -- double checked the code 20 times... Turned out to be an interpreter bug*.
Here's the self-contained code (it has to do with PE problem 196): [CODE]n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p),\ a1=isprime(p+n+1);a2=isprime(p-n+1);if((a2&&(a1||isprime(p-n*2+4)))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) )));s %8 = 71089179309899646 n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p),\ a1=isprime(p+n+1);a2=isprime(p-n+1);if(a2&&(a1||isprime(p-n*2+4))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) )));s %10 = 53760184371086923 [/CODE] The only difference is extra parens around "a2&&(a1||isprime(p-n*2+4))" which is joined by ||, so that shouldn't matter, but it does. /sigh/ *[SIZE="1"]Granted that this was an old stable version 2.3.5 which I'd used for years. I'll check if this is still a bug in 2.5.4, [STRIKE]and if it is, will report to them[/STRIKE]. No, it has been fixed![/SIZE] EDIT: No it hasn't! 2.5.4 still produces wrong results in a slightly different condition set for the second half of the problem. Works if you wrap every single expression in parens, even in something like [B](a&&b)||(c&&d)||(e&&f)[/B]. [U]Will report[/U]. |
v2.5.0: (which I am currently using)
[CODE] (11:21:57) gp > n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p),\ a1=isprime(p+n+1);a2=isprime(p-n+1);if((a2&&(a1||isprime(p-n*2+4)))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) )));s %1 = 79697256800321526 (11:23:07) gp > n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p),\ a1=isprime(p+n+1);a2=isprime(p-n+1);if(a2&&(a1||isprime(p-n*2+4))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) )));s %2 = 79697256800321526 (11:23:57) gp > [/CODE] Are you sure is not something related to the partial evaluation of boolean expressions? ("default" settings?) |
Try these equivalent expressions:
1. fully dressed with parens (Ugh!) [CODE]n=7208785;s=0; forstep(p=n*(n-1)/2+5,n*(n+1)/2-1,[2,4],if(!isprime(p),next);\ if(p%6==1,a3=isprime(p-n+1);a4=isprime(p+n-1);if((a3&&(a4||isprime(p-n-n+2)))||[COLOR="Blue"](a4&&isprime(p-2))[/COLOR],s+=p);next);\ a1=isprime(p-n+1);a4=isprime(p+n+1);a5=isprime(p+n-1);\ if((a1&&(a4||a5||isprime(p-n-n+2)||isprime(p-n-n+4)))||\ ((a4&&(a5||isprime(p+2)))||(a5&&isprime(p+n+n))),s+=p) );s [/CODE] and 2. One condition is left naked: [CODE]n=7208785;s=0; forstep(p=n*(n-1)/2+5,n*(n+1)/2-1,[2,4],if(!isprime(p),next);\ if(p%6==1,a3=isprime(p-n+1);a4=isprime(p+n-1);if((a3&&(a4||isprime(p-n-n+2)))||[COLOR="Red"]a4&&isprime(p-2)[/COLOR],s+=p);next);\ a1=isprime(p-n+1);a4=isprime(p+n+1);a5=isprime(p+n-1);\ if((a1&&(a4||a5||isprime(p-n-n+2)||isprime(p-n-n+4)))||\ ((a4&&(a5||isprime(p+2)))||(a5&&isprime(p+n+n))),s+=p) );s[/CODE] 3. try undressing more clauses from (a&&b)||(c&&d)||(e&&f) into (a&&b)||(c&&d)||(e&&f) (a&&b)||c&&d||e&&f a&&b||c&&d||e&&f ==> totally random results! |
[QUOTE=Batalov;344089]Try these equivalent expressions:[/QUOTE]
It's Friday night (or Saturday morning; depending on where one finds themselves), and rather than getting laid some of us are finding bugs in software (and having fun doing so). And to think some others think we're a bit "nerdy".... :smile: |
Gotta get me a genie to half my age, and then we could talk about priorities! ;-)
Currently, I am more concerned about AST/ALT ratios and with philosophical questions like "Do I prefer to follow Tony S. with a heart attack if I don't take my statins or do I want to follow David E. with failed liver if I do take my statins?" Getting laid is a little bit down the list at this time. Seriously though, today, after a month off of (prescription) drugs, AST and ALT are both back to normal, but CHL is way up. [URL="http://mersenneforum.org/showthread.php?t=9862&highlight=whisky+consumption"]Crap, crap, crap[/URL]! |
[QUOTE=Batalov;344089]==> totally random results![/QUOTE]
No, no, that is what I am trying to explain to you. Because of "partial evaluation" involved in pari (and in C also), of the boolean expressions, and because in pari the && and || have the same priority (both are class 0, see the section 2.4.2 in the user manual, as opposite for example to MQL, where || is more prioritar than &&, which is a bit counterintuitive, but if you think about what MQL is for, it makes sense), the expressions are NOT equivalent. Pari stops evaluating the boolean expressions when the value is certain. For example, in "a || b && c", when you partial evaluate it, this is always true if "a" is true, if you evaluate it from the left (like pari says it does, in the docs) and is always false if "c" is false and you evaluate it from the right, but you can't say nothing about "b", it depends where do you pair it (how do you associate). There is no bug here, you set your pari to fully evaluate boolean expressions, left to right, or better (and safer) [COLOR=Red][B]use parenthesis[/B][/COLOR]! On the default installation, this is [U]NORMAL[/U], even with [U]full evaluation[/U]: [CODE] (11:23:57) gp > for(a=0,1,for(b=0,1,for(c=0,1,print(a" "b" "c" | "a&&b||c" <-> ",(a&&b)||c)))) 0 0 0 | 0 <-> 0 0 0 1 | 1 <-> 1 0 1 0 | 0 <-> 0 0 1 1 | 1 <-> 1 1 0 0 | 0 <-> 0 1 0 1 | 1 <-> 1 1 1 0 | 1 <-> 1 1 1 1 | 1 <-> 1 (12:17:19) gp > for(a=0,1,for(b=0,1,for(c=0,1,print(a" "b" "c" | "a||b&&c" <-> ",a||(b&&c))))) 0 0 0 | 0 <-> 0 0 0 1 | 0 <-> 0 0 1 0 | 0 <-> 0 0 1 1 | 1 <-> 1 [COLOR=Red][B]1 0 0 | 0 <-> 1[/B][/COLOR] 1 0 1 | 1 <-> 1 [B][COLOR=Red]1 1 0 | 0 <-> 1[/COLOR][/B] 1 1 1 | 1 <-> 1 (12:17:48) gp >[/CODE]for records, this is version 2.5 |
[QUOTE=LaurV;344098]...(and in C also)...[/QUOTE]
Incorrect for C. In C, the rules of precedence are [URL="http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence"]clear[/URL], never mind partial evaluation (partial evaluation goes without saying or else all programs would be twice as long). For Pari, I see what you are saying. Indeed, they lumped both operations in the same precedence. This is an awful, naughty catch. I am very surprised that they didn't assign the same precedence to * and +, then. (but they didn't! thank god for that). So they are right to call this a "calculator". Like windows calc: 1+2*3, in "Simple" and "Scientific" modes. It is 9 and 7. Disgusting. && to || is like * to +. But then again, with programmers, you can never be sure how they will parse your statement. It is like in the anecdote where the wife instructs the programmer: "At the grocery, buy two cabbages and if they have eggs buy a dozen." The programmer returns with a dozen cabbages! |
[QUOTE=Batalov;344103]But then again, with programmers, you can never be sure how they will parse your statement. It is like in the anecdote where the wife instructs the programmer: "At the grocery, buy two cabbages and if they have eggs buy a dozen." The programmer returns with a dozen cabbages![/QUOTE]
That was a system malfunction... How the programmer decided if the cabbages have eggs? Anyhow, the result is wrong in all cases: if no eggs, he should come with two cabbages. If someone had eggs (the grocery, the cabbage, the seller, whatever), then the guy should return with 14 cabbages... :razz: |
[QUOTE=Batalov;344103]Incorrect for C[/QUOTE]
Correct for C. I was referring to partial evaluation only. I brought the "precedence" into discussion only later, specifically writing "in pari" for a second time in the same sentence! As in "in pari the && and || have the same priority". The reference to C was only to clarify for you what I mean by "partial evaluation", as I know you can handle C very well. The fact that partial evaluation and total evaluation give same results when a single operation is used, (like only &&, or only ||, but not both) has nothing to do with C or with pari, it is just a consequence of the property of the operation itself (change that to xor, and everything changes!). When the precedences are different, you practically have a single operation (because you solve the most priory operation first), but when the precedence is the same, you mix the operations. In this respect, pari has no bug: everything works as in the specs (user manual). Use parenthesis! :razz: |
The more I think about this Pari implementation, the more I get the feeling that today I looked in the abyss.
Let's think about this: why is it that every language (clearly, except Pari !!) has a clear distinction of priorities between these two operations (&& and ||, or say, in python or SQL - "and" and "or")? Look them up, from Fortran to ruby, to SQL, from java to C-[STRIKE]bemol[/STRIKE]-diez. Because logical operators implement logic, and there cannot be anything more basic than logic. [URL="http://en.wikipedia.org/wiki/Logical_connective#Properties"]You don't fsck with logic[/URL]! Ok, Let's consider a&&(b||c) vs a||(b&&c) regardless of order, that is the same as (b||c)&&a vs (b&&c)||a. What is the main difference? Never mind lazy evaluation or side-effects (when a,b,c are mutable objects and you sandwich operators into logic, like if(++c && (++a==0 || ++b==0)) or in Perl-style, where $p is assumed to be a ref to an array, $p->[3]-- if($p && @$p>3 && $p->[3]>1) ). No, let's consider plain logic evaluation with a, b, and c defined and immutable objects with an associated defined logic value. If it is plain, and there are no side-effects, there is no difference between partial and full evaluation - the truth value will be the same, only the compute time is saved. But even though a&&(b||c) = a&&b || a&&c, in contrast a||(b&&c) = a || b&&c, period. So the operator precedence is not something you assign at a whim or out of laziness, it is imposed by logic that you are supposed to implement. Finally, with Pari, what's even more horrible that the results are variable between versions. I could be conditionally fine with just the implementation by the rule: "in our Pari world, we evaluate left from right all precedence 0 operators (which are && and ||) and that's final." But it's not! The results of the above snippets are all randomly different between different Pari binaries that I have fetched. I am deeply saddened. |
[QUOTE=LaurV;344110]That was a system malfunction... How the programmer decided if the cabbages have eggs? Anyhow, the result is wrong in all cases: if no eggs, he should come with two cabbages. If someone had eggs (the grocery, the cabbage, the seller, whatever), then the guy should return with 14 cabbages... :razz:[/QUOTE]
The seller didn't have eggs... he had balls! :davieddy: |
And that is how your world turns upside-down :razz:
As a programmer-for-living for few decades, changing my tools many times (sometime few times per year, depending of the projects I was working for) I learned a good lesson: you have to know your tools. You would be surprised how many other languages "fsck up with logic". As I already [URL="http://docs.mql4.com/basis/operations/rules"]mentioned MQL[/URL], you can see for yourself that || take precedence to && (which, to my feeling, hurts more than putting all in the same bowl). But well, everything has a reason. However, after people insisted (we were discussing there, same as me and you discussing here now) they [URL="http://www.mql5.com/en/docs/basis/operations/rules"]changed it in version 5[/URL] of the language, to a more mathematical way. Staying on pari, you will be even more confused when you will find out that there are no bitwise & and | operators, and in fact they are equivalent to && and respective ||, hehe. |
Yes, but you don't see them break their own written rules, right?
Run the code with different Pari binaries, and you will see what upsets me. Most wonderful of course is perl! "There's more than one way to do it! " (c) Because sometimes they wanted "and" and "or" to have lowest possible precedence*, they have added "and" and "or" in addition to "&&" and "||", and all the difference [B]is [/B]precedence. And they didn't break the rules, they just made more rules. But that's honest in my book. _________ * obligatory example: [CODE]open IN, "<file" or die;[/CODE] ;-) |
[QUOTE=Batalov;344117]Yes, but you don't see them break their own written rules, right?
Run the code with different Pari binaries, and you will see what upsets me. Most wonderful of course is perl! "There's more than one way to do it! " (c) Because sometimes they wanted "and" and "or" to have lowest possible precedence*, they have added "and" and "or" in addition to "&&" and "||", and all the difference [B]is [/B]precedence. And they didn't break the rules, they just made more rules. But that's honest in my book. _________ * obligatory example: [CODE]open IN, "<file" or die;[/CODE] ;-)[/QUOTE] [code]? n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p), a1=isprime(p+n+1);a2=isprime(p-n+1);if(a2&&(a1||isprime(p-n*2+4))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) )));s %5 = 79697256800321526 ? n=5678027;s=0;forstep(p=n*(n-1)/2+4,n*(n+1)/2-1,[2,4],if(isprime(p),\ if(p%6==1,a1=isprime(p+n-1);a3=isprime(p+n+1);if(a1&&(a3||isprime(p-2)||isprime(p+n+n))||(a3&&isprime(p+n+n+2)),s+=p), a1=isprime(p+n+1);a2=isprime(p-n+1);if((a2&&(a1||isprime(p-n*2+4)))||(a1&&(isprime(p+2)||isprime(p+n+n+2))),s+=p) ))); %6 = 79697256800321526[/code] works in 2.5.3 |
[QUOTE=LaurV;344115]Staying on pari, you will be even more confused when you will find out that there are no bitwise & and | operators, and in fact they are equivalent to && and respective ||, hehe.[/QUOTE]
& and | are deprecated, and have been removed in recent versions. GP does have bitwise operators, just not convenient C-style ones: bitor, bitand, bitxor, bitnegimply. |
[QUOTE=CRGreathouse;344177]& and | are deprecated, and have been removed in recent versions.
GP does have bitwise operators, just not convenient C-style ones: bitor, bitand, bitxor, bitnegimply.[/QUOTE] I've been playing around with these, is there a more reliable way using them to add 2 to a number than: [CODE] bitneg(bitxor(bitneg(x),2)) [/CODE] ? |
[code]x+2[/code]
? Maybe if you tell me what you're trying to accomplish... |
[QUOTE=CRGreathouse;344412][code]x+2[/code]
?[/QUOTE] I was thinking more with the bit operators since in some cases it seems quicker with them. I'm just simply trying to accomplish an addition of 2 just seemed to be fun instead of a forstep loop. edit:though using it for gaps between primes might be fun ( since they divide by 2) |
[QUOTE=science_man_88;344413]I was thinking more with the bit operators since in some cases it seems quicker with them.[/QUOTE]
Doubtful. [code]> for(x=1,1e6,bitneg(bitxor(bitneg(x),2))) time = 734 ms. > for(x=1,1e6,x+2) time = 428 ms.[/code] The bitwise versions need to allocate space and then copy three times instead of once, so asymptotically I expect them to take nearly three times as long. |
Few weeks ago, I started learning PARI, then some day, I was needed to make a vector turns into an integer, I know it's possible to make an integer turn into an array using the function "digits(n)", but I need to know if somehow I can reverse it. For example: if the vector a=[1,2,3], it will turn into a=123. If the vector b=[12,3,45], it will turn into b=12345.
Thanks for anyone who can help me. :smile: |
[QUOTE=ismillo;346922]Few weeks ago, I started learning PARI, then some day, I was needed to make a vector turns into an integer, I know it's possible to make an integer turn into an array using the function "digits(n)", but I need to know if somehow I can reverse it. For example: if the vector a=[1,2,3], it will turn into a=123. If the vector b=[12,3,45], it will turn into b=12345.
Thanks for anyone who can help me. :smile:[/QUOTE] function names depend on version of pari and I'm no expert but: [CODE] a=[1,2,3];b=#a;until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));if(#a==b,a[b-1]=a[b]);a=vector(#a-1,n,a[n]));a=eval(a[1]) [/CODE] appears to work for me ( but is pretty complicated in my mind) ( edit:no it doesn't I made an error): to go the same way as your digits function: [CODE]a=123;a=eval(Vec(Str(a)))[/CODE] works |
[QUOTE=science_man_88;346974] [CODE]
a=[1,2,3];b=#a;until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));if(#a==b,a[b-1]=a[b]);a=vector(#a-1,n,a[n]));a=eval(a[1]) [/CODE] [/QUOTE] the correct version is: [CODE]a=[1,3,7,15,31,63];until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));for(b=3,#a,a[b-1]=a[b]);a=vector(#a-1,n,a[n]));a=eval(a[1])[/CODE] |
Thank you very much, Sir. This will help a lot.
PARI is a great languague for mathematics no doubts, however let's be honest, it's some times so much complicated. For anyone who has the same problem I had, here is the function, only if you may, science_man_88, all credits for you. [CODE] vectoint(a)={ until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));for(b=3,#a,a[b-1]=a[b]); a=vector(#a-1,n,a[n])); a=eval(a[1]) }; [/CODE] Once again, thank you very much, for solving my problem and for quick reply as well. |
[CODE]vec2int(a)={my(s=""); for(i=1,#a,s=Str(s,a[i])); eval(s)}[/CODE]
|
1 Attachment(s)
Another great help, axn, thank you.
I was doing some test here, axn is a faster, however, both are excellent and helps a lot. [ATTACH]10041[/ATTACH] |
There's an even faster version available if your numbers are only a digit each:
[code]subst(Pol(a), 'x, 10)[/code] |
Awesome, this will help a lot with large palindromics numbers.
I usually compare palindromes making vectors and never knew how I could manipulate the numbers. Edit: One last question, how do I return the position of a specific character from a word? For example 'a = "ABC"' and return "A". PS.: I know PARI isn't the right language for strings, but in the end, it's all about Mathematics. |
[QUOTE=ismillo;347172]One last question, how do I return the position of a specific character from a word?
For example 'a = "ABC"' and return "A". PS.: I know PARI isn't the right language for strings, but in the end, it's all about Mathematics.[/QUOTE] PARI is bad at this. The best method it has to offer is Vec("ABC")[1]. For finding a string, you can try [code]find(source, target)={ source=Vec(source); target=Vec(target); for(i=0,#source-#target, for(j=1,#target, if(target[j]!=source[i+j],next(2)) ); return(i+1) ); -1 };[/code] |
[QUOTE=ismillo;347172]Awesome, this will help a lot with large palindromics numbers.
I usually compare palindromes making vectors and never knew how I could manipulate the numbers.[/QUOTE] I should also mention the commands Vecrev and Polrev, then. [code]subst(Polrev(digits(123)), 'x, 10)[/code] gives 321. |
[QUOTE=CRGreathouse;347187]I should also mention the commands Vecrev and Polrev, then.
[code]subst(Polrev(digits(123)), 'x, 10)[/code] gives 321.[/QUOTE] Nice, I didn't knew I could reverse vector with function from PARI, so I made my own. :D [QUOTE=CRGreathouse;347185]PARI is bad at this. The best method it has to offer is Vec("ABC")[1]. For finding a string, you can try [code]find(source, target)={ source=Vec(source); target=Vec(target); for(i=0,#source-#target, for(j=1,#target, if(target[j]!=source[i+j],next(2)) ); return(i+1) ); -1 };[/code][/QUOTE] This is perfect, I guess. Later I will give it a shot, if I succeed then I notify you guys. |
Well, I'm having an issue with 2 equal letters, I'll keep trying, if can't do it I will try make in C.
:/ |
[QUOTE=ismillo;347250]Well, I'm having an issue with 2 equal letters, I'll keep trying, if can't do it I will try make in C.
:/[/QUOTE] Can you give an example of the input, the expected output, and the actual output? I'll admit that I didn't test my script at all, just typed it up, so bugs are certainly possible. |
Sure.
I don't know if you ever heard about Project Euler, It's a website about Math problems which is highly recommend use programming languages. So, there is this problem, you have an array of words and you sum each letter (A=1;B=2;C=3;...Z=26;(For example "CAN" = 18; 3+1+14=18)). If the sum of the letters is in the possibles results from the equation "tn = ½n(n+1)", then the word is a triangle word. The problem is asking for how many triangle words the array has. My issue is, there's some words with two equal letter, such as "ADD" and "ACCESS", however the function only detects one and only sums as one. I'm not asking for the answer, I'm just asking for help. ---------------------------------------------------- INPUT a="ABCA"; find(a,"A"); OUTPUT 1 Only returns the first word that match with. |
So you don't want a find function at all, but just (1) a function that loops over the letters, and (2) a function that converts a letter to its numeric value. Function (1) calls function (2) each time through the loop and returns the sum.
I'd suggest something like [code]getLetterValue(letter)={ \\ something here }; getWordValue(word)={ my(letters=Vec(word)); sum(i=1,#letters, getLetterValue(letters[i])) };[/code] |
Well, while I was sleeping I have found the solution.
I knew the "Vec(x)" exists, however, I didn't have the slightest idea it could split words into its characters. Thanks, CRG. Here's the code. [CODE] counterWords(w)={ l=Vec(w); vp=vector(26,i,Strchr(i+64)); c=0; for(a=1,#l,c+=vecsearch(vp,l[a])); c; } w=read("words.txt"); t=0; vt=vector(18,n,(1/2*n*(n+1))); for(x=1,#w,c=counterWords(w[x]);if(vecsearch(vt,c),t++));t [/CODE] |
Good, you got it working.
You might find that using Vecsmall("ABC") is more convenient... you can just subtract 64 and be done, no need for a lookup table. |
Great, this new function is about 50% faster than the old one. :D
[CODE] cw(w)={ l=Vec(w); c=0; for(a=1,#l,c+=Vecsmall(l[a])[1]-64);c; } w=read("words.txt"); t=0; vt=vector(18,n,(1/2*n*(n+1))); for(x=1,#w,c=cw(w[x]);if(vecsearch(vt,c),t++));t [/CODE] |
[CODE]cw(w)={
my(v=Vecsmall(w)); sum(i=1,#v,v[i]-64) };[/CODE] should be much faster. You could improve this to [CODE]cw(w)={ my(v=Vecsmall(w)); sum(i=1,#v,v[i])-64*#v };[/CODE] at a slight cost to readability. Also [code]sum(i=1,#w,ispolygonal(cw(w[i]),3))[/code] if you're using a recent version of gp. |
I see, now, the 1st function solve the problem in 40 ms, the 2nd in 20 ms.
The two new one solve in 14 ms the 1st, and 13 the 2nd. "sum(i=1,#w,ispolygonal(cw(w[i]),3))" solves in 3 ms. However its specific for this situation. So, this is a great improvement. PS.: "Sum" is better than "for" in every situation which requires repetitions? |
[QUOTE=ismillo;347388]"Sum" is better then "for" in every situation?[/QUOTE]
When you're doing a sum, use sum. When you're doing a product, use prod. that way you don't need temporary variables so it looks neater (and is shorter to type). Plus, as an added benefit, prod uses binary splitting to keep subproducts small which can greatly improve speed. |
Hm... understood. Soon or later I'll probably keep reading PARI docs. I was just using what I knew. It's difficult to find good tutorials around the web, you guys and this thread is helping a lot.
Thank you, Sir. |
Glad to help. You may also find
[url]http://rosettacode.org/wiki/Category:PARI/GP[/url] useful. It contains a large number of tasks and their solutions in GP (as well as many other languages). I actually wrote most of the PARI/GP code myself, in hopes that it would help people like you. Also, you'll find some tutorials linked there -- possibly useful as well. (I didn't write any of them.) |
Rosetta Code is very helpful. Couple weeks ago (in the same week the website was down), I was listing all possible useful functions and put them all in a file. :D
|
Oh, another general tip: don't use variables in a function without declaring them with my(). You'll avoid a lot of trouble this way.
So I wrote [code]cw(w)={ my(v=Vecsmall(w)); sum(i=1,#v,v[i]-64) };[/code] instead of [code]cw(w)={ v=Vecsmall(w); sum(i=1,#v,v[i]-64) };[/code] which means that if I type [code]v=1;w=cw("ABC");v[/code] I get 1 instead of Vectorsmall(65, 66, 67). There are other reasons to use my() other than just variable clobbering. For example, the scripts tend to be faster and are compatible with gp2c. But don't worry about the reasoning, just get into the habit of doing this. :smile: |
Oh, Thanks for the hint, I was wondering why was returning Vectorsmall(x).
I have a few more questions. 1) Does PARI has a friendly developing interface? I have found "Pari-tty", however, its not up-to-date (it runs only with PARI 2.2.11). 2) Somehow can I return the decimal fraction of a number? For exemple: [TEX]\pi[/TEX]=3.1415[...]. I want only the "1415[...]". Once again, Thank you. |
[QUOTE=ismillo;347475]1) Does PARI has a friendly developing interface? I have found "Pari-tty", however, its not up-to-date (it runs only with PARI 2.2.11).[/QUOTE]
I consider gp pretty friendly. :ermm: [QUOTE=ismillo;347475]2) Somehow can I return the decimal fraction of a number? For exemple: [TEX]\pi[/TEX]=3.1415[...]. I want only the "1415[...]".[/QUOTE] You could write a function to do that. [code]fracpart(x)=x - floor(x)[/code] or even [code]fracpart(x)=x - x\1[/code] where \ means "divide and round down". |
.-.
I have several issues with Command Prompt interface , such as copy and paste (right-click all time is a lame). Does not show all the code writen before the last hundred lines (up arrow key all time is a lame). I don't know if it's because I'm not used to, or lazy, or too new in this area. Or all of them. Btw, I did't even think in doing this function. Now it's 1am, later today I'll try something with this. Thanks. |
[QUOTE=ismillo;347485]I have several issues with Command Prompt interface , such as copy and paste (right-click all time is a lame).[/QUOTE]
If you're in Windows, right-click the shortcut that starts the program and click "Quick Edit" on the appropriate tab. This should allow you to copy with the mouse + Enter and paste with a right click. Copy/paste should work in Linux out of the box (but if you're having trouble let me know). [QUOTE=ismillo;347485]Does not show all the code writen before the last hundred lines (up arrow key all time is a lame).[/QUOTE] Write programs in your favorite text editor and then just read the file into gp with \r filename. You can even edit the gprc.txt or .gprc file to automatically read this file when you start your session to make this more convenient. See [url]http://math.crg4.com/pari/highlight.html[/url] for syntax highlighting if your text editor does not have this already. If you're on a friend's computer (or at school, work, etc. and can't install programs) you can get syntax highlighting online at pastebin.com. |
| All times are UTC. The time now is 23:20. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.