![]() |
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! |
Edit: kar_bon explains this above, see post #1101.
[QUOTE=science_man_88;227554]that's why mine gave me times of 10-20 ms but the other gave me 600-650? ms[/QUOTE] 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",")))[/code] for example. |
[QUOTE=CRGreathouse]Ah, so you just don't understand the word "redundancy".
[/QUOTE] 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. |
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.
|
[QUOTE=CRGreathouse;227557]
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",")))[/code][/QUOTE] 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 ? |
[QUOTE=3.14159;227558]There are no redundancies.
Not every square number is a fourth power. Fourth powers are a subset of squares.[/QUOTE] 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=3.14159;227558]Nevermind. This is getting me nowhere. Fine, you win. You can keep the smug expression on your face.[/QUOTE] Nah, I really only look smug when I'm showing a trivial (counter)example. |
[QUOTE=science_man_88;227559]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.[/QUOTE]
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. |
[QUOTE=science_man_88;227560]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.[/QUOTE]
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=science_man_88;227560]you use & not && why ?[/QUOTE] They're exactly the same in Pari; I occasionally use one and occasionally the other. [QUOTE=science_man_88;227560]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[/QUOTE] I'm betting that if you look at my code more closely you'll be able to figure this one out. |
[QUOTE=CRGreathouse;227563]I'm betting that if you look at my code more closely you'll be able to figure this one out.[/QUOTE]
"forprime(p=2,1e9,n=p-1 (...)" is the relevant part! Good job! |
[QUOTE=kar_bon;227564]"forprime(p=2,1e9,n=p-1 (...)" is the relevant part! Good job![/QUOTE]
so in other words it only works when n is 1 less than a prime ? |
[QUOTE=science_man_88;227566]so in other words it only works when n is 1 less than a prime ?[/QUOTE]
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.) |
| All times are UTC. The time now is 23:13. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.