mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

Batalov 2013-06-22 08:02

[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:

LaurV 2013-06-22 08:09

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.

Batalov 2013-06-22 08:16

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] ;-)

science_man_88 2013-06-22 12:10

[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

CRGreathouse 2013-06-23 01:47

[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.

science_man_88 2013-06-25 22:36

[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]

?

CRGreathouse 2013-06-25 22:38

[code]x+2[/code]
?

Maybe if you tell me what you're trying to accomplish...

science_man_88 2013-06-25 22:40

[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)

CRGreathouse 2013-06-25 22:42

[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.

ismillo 2013-07-22 00:53

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:

science_man_88 2013-07-22 13:14

[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


All times are UTC. The time now is 22:37.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.