mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-08-29, 19:41   #1101
kar_bon
 
kar_bon's Avatar
 
Mar 2006
Germany

22×727 Posts
Default

The expression "if ((a==true) * (b==true) * (c==true) == 1)" depends also on the interpreter/compiler.

If the first expression is false (a=false), the whole expression won't be true anymore (if true treated as '1' and false as '0')!

So the other two comparisons (b and c) could be omitted and that would be really faster!

Last fiddled with by kar_bon on 2010-08-29 at 19:44
kar_bon is offline   Reply With Quote
Old 2010-08-29, 19:44   #1102
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Edit: kar_bon explains this above, see post #1101.

Quote:
Originally Posted by science_man_88 View Post
that's why mine gave me times of 10-20 ms but the other gave me 600-650? ms
Ah, I see what you're saying. No, not really -- the savings is the fact that you don't calculate isprime() more than needed. cmp isn't what's at issue here -- actually, your method introduces an expensive branch operation (JZ, probably). But much better to have a JZ and risk throwing out your pipeline than calculate another isprime(), which will take thousands of cycles in the best case.

But if you want fast code you can do better. Try
Code:
forprime(p=2,1e9,n=p-1;if(isprime(n^2+1)&isprime(n^4+1),print1(n",")))
for example.

Last fiddled with by CRGreathouse on 2010-08-29 at 19:45
CRGreathouse is offline   Reply With Quote
Old 2010-08-29, 19:47   #1103
3.14159
 
3.14159's Avatar
 
May 2010
Prime hunting commission.

24×3×5×7 Posts
Default

Quote:
Originally Posted by CRGreathouse
Ah, so you just don't understand the word "redundancy".
There are no redundancies.

Not every square number is a fourth power. Fourth powers are a subset of squares.

Nevermind. This is getting me nowhere. Fine, you win. You can keep the smug expression on your face.

Last fiddled with by 3.14159 on 2010-08-29 at 19:49
3.14159 is offline   Reply With Quote
Old 2010-08-29, 19:47   #1104
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

the code is for pari and no regardless of if the first is one this code uses imul() 2 times and a cmp so about 9 cmp equivalent regardless mine uses 3 cmp and 2 and commands equivalent so unless each and is triggered every time and takes 3 cmp each my code is faster karsten.

Last fiddled with by science_man_88 on 2010-08-29 at 19:48
science_man_88 is offline   Reply With Quote
Old 2010-08-29, 19:50   #1105
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post

But if you want fast code you can do better. Try
Code:
forprime(p=2,1e9,n=p-1;if(isprime(n^2+1)&isprime(n^4+1),print1(n",")))
this only does better under primelimit if it goes over it throws an error mine can go to at least primelimit^2 but I could check if I can speed mine up more lol. also this is about same timing as mine for one test and unless there's a law if n^2+1 is prime and n^4+1 is prime then n+1 is prime this isn't what solves it and you use & not && why ?

Last fiddled with by science_man_88 on 2010-08-29 at 19:54
science_man_88 is offline   Reply With Quote
Old 2010-08-29, 19:52   #1106
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
There are no redundancies.

Not every square number is a fourth power. Fourth powers are a subset of squares.
It is *because* fourth powers are a subset of squares that the expression is redundant. You're arguing that it's not redundant because it's not the same as "x is a square", but since it *is* the same as "x is a fourth power" the expression is redundant.

Quote:
Originally Posted by 3.14159 View Post
Nevermind. This is getting me nowhere. Fine, you win. You can keep the smug expression on your face.
Nah, I really only look smug when I'm showing a trivial (counter)example.

Last fiddled with by CRGreathouse on 2010-08-29 at 20:01
CRGreathouse is offline   Reply With Quote
Old 2010-08-29, 19:57   #1107
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
the code is for pari and no regardless of if the first is one this code uses imul() 2 times and a cmp so about 9 cmp equivalent regardless mine uses 3 cmp and 2 and commands equivalent so unless each and is triggered every time and takes 3 cmp each my code is faster karsten.
Don't be too quick to translate Pari code to assembly. There's quite a bit of overhead associated with using multiprecision numbers and dynamic typing.

But in this case that's not really the point -- the point is that your code is better than the one given in the sequence because it does less work (not checking the larger numbers for primality if the smaller ones fail), *not* because of the cmp vs. imul. That difference would be too small to measure with that few iterations.
CRGreathouse is offline   Reply With Quote
Old 2010-08-29, 20:00   #1108
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
this only does better under primelimit if it goes over it throws an error mine can go to at least primelimit^2 but I could check if I can speed mine up more lol.
You're better off increasing primelimit in that case. I just sent in a b-file with the first 10,000 terms; it took 12.2 seconds + 3.8 seconds for increasing primelimit. Your code (appropriately modified to store the results in an array the same way I did with mine) took 35.4 seconds.

Edit: I could have reduced the time needed for the primelimit calculation to 90 ms by reducing the bound to 31370407...

Quote:
Originally Posted by science_man_88 View Post
you use & not && why ?
They're exactly the same in Pari; I occasionally use one and occasionally the other.

Quote:
Originally Posted by science_man_88 View Post
also this is about same timing as mine for one test and unless there's a law if n^2+1 is prime and n^4+1 is prime then n+1 is prime this isn't what solves it
I'm betting that if you look at my code more closely you'll be able to figure this one out.

Last fiddled with by CRGreathouse on 2010-08-29 at 20:13
CRGreathouse is offline   Reply With Quote
Old 2010-08-29, 20:03   #1109
kar_bon
 
kar_bon's Avatar
 
Mar 2006
Germany

22·727 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
I'm betting that if you look at my code more closely you'll be able to figure this one out.
"forprime(p=2,1e9,n=p-1 (...)" is the relevant part! Good job!
kar_bon is offline   Reply With Quote
Old 2010-08-29, 20:14   #1110
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by kar_bon View Post
"forprime(p=2,1e9,n=p-1 (...)" is the relevant part! Good job!
so in other words it only works when n is 1 less than a prime ?
science_man_88 is offline   Reply With Quote
Old 2010-08-29, 20:17   #1111
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
so in other words it only works when n is 1 less than a prime ?
Instead of testing if n+1 is a prime, I loop through the primes, choosing n = p-1 so that n+1 is prime. Because I'm using Pari's precalculated list instead of testing numbers, the search is much faster. (This occurs even if I have to generate the whole list myself -- sieving is much faster than testing.)
CRGreathouse is offline   Reply With Quote
Reply

Thread Tools


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

All times are UTC. The time now is 23:13.


Fri Aug 6 23:13:12 UTC 2021 up 14 days, 17:42, 1 user, load averages: 4.49, 4.28, 4.08

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

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.