![]() |
[QUOTE=CRGreathouse;347566]Cool, I look forward to it![/QUOTE]
reading a possibly old manual it seems there's so many different ways to get the same answer: squaring x ( it seems) [CODE]norm(x) \\ for integers at least sqr(x) x^2[/CODE] multiply x by 2 (it seems) [CODE]trace(x) 2*x x<<1[/CODE] how many that can you the same thing are there ? |
[QUOTE=science_man_88;347727]reading a possibly old manual it seems there's so many different ways to get the same answer:
squaring x ( it seems) [CODE]norm(x) \\ for integers at least sqr(x) x^2[/CODE] multiply x by 2 (it seems) [CODE]trace(x) 2*x x<<1[/CODE] how many that can you the same thing are there ?[/QUOTE] Lots of ways. But be careful, these aren't quite equivalent. trace(I) == 0, for example. |
So, about the highlight, I couldn't make it, since it's necessary JSON and I don't know nothing about it.
Now, about the fractional part to integer, I could make this: [CODE] r2i(a)={ my(b=Vec(Str(a)),c=b[#Str(ceil(a))+2..#b],d=eval(c)); forstep(x=#d,1,-1,if(d[x]!=0,h=x;break;)); e=vecextract(c,[1..h]); x=eval(e); v2i1d(x) } [/CODE] v2i1d(x) is CRG function for vectors to integer with one digit each position. [CODE] v2i1d(a)=subst(Pol(a), 'x, 10) [/CODE] It has some problems, however, I think it will handle ok. |
If the last part of your script is just removing trailing zeros, you can do that faster with
[code]removeTrailingZeros(n)={ n / 10^valuation(n, 10) };[/code] |
[QUOTE=CRGreathouse;348149]If the last part of your script is just removing trailing zeros, you can do that faster with
[code]removeTrailingZeros(n)={ n / 10^valuation(n, 10) };[/code][/QUOTE] This is awesome. Work like a charm. Thanks. :D [CODE] removeTrailingZeros(n)={ n / 10^valuation(n, 10) }; v2i1d(a)=subst(Pol(a), 'x, 10); r2i(a)={ my(b=Vec(Str(a)),c=b[#Str(ceil(a))+2..#b],d=eval(c)); removeTrailingZeros(v2i1d(d)) }.; [/CODE] |
factorisation
Hi, i just started learning pari and im trying to use store the prime factor of a value. im using a=factor(1599) but it stores only the results of the prime factor. can someone give me some guidance on how i could store any of the prime factor value to a variable?
|
Pari/gp command
hi, im new to pari/gp and i need some guidance on how to go about storing my factor value into a variable. i have for example 192384 and i need to get 1 of the prime factor of this value.
i tried storing it to a variable "a=factor(192384)" but it stores all the prime value to it. can anyone guide me how i could get 1 of the prime factor please. |
I was trying to look up a list of mersenne factors somewhere else online and by Google search got to this code on rosettacode.org but there's some parts I don't understand can anyone help explain it ?
[CODE]factorMersenne(p)={ forstep(q=2*p+1,[COLOR="red"]sqrt(2)<<(p\2)[/COLOR],2*p, [COLOR="Red"][1,0,0,0,0,0,1][q%8][/COLOR] && Mod(2, q)^p==1 && return(q) ); 1<<p-1 };[/CODE] I don't understand why that's the limit, I get it as sqrt(2)^(p+1) so is that from some part of the p+1 test ? as to the other part that's red I tried q=10 by itself for this part of the code and I see it changes the answer from 2 to 0, but I can't even speculate on this part. edit: doh it's a call to the vector shown there should be an easier way. |
sqrt(2)<<(p\2) = sqrt(2) * 2^((p-1)/2)which is a fancy way of saying sqrt(Mp). [a << b = a left shifted b bits]
[1,0,0,0,0,0,1][q%8] is a shorthand way of saying v=[1,0,0,0,0,0,1]; v[q%8]. Just a way to test if q%8 == 1 or 7 |
When you check for factors of a number, you only need to check up to square root of the number. The limit is an obfuscated way to write sqrt(2^p-1), or sqrt(1<<p-1), if you like, which might be a bit faster because of the internal pari representation. In fact, its speed depends on the number of decimals, and it could be much faster if you first set default(realprecision,5), get the root and set the realprecision back. The backslash is integer division. Anyhow, the speed of this calculus is not relevant, because it is only executed once in the beginning, and only takes few milliseconds.
The "vectored" part you can understand it better if you write: v=[9,8,7,6]; a=v[2] This will return 8. Now imagine you don't use v, but write directly the constant, as "a=[9,8,7,6][2]", this will still return 8. (indexing starts with 1) The faster way to write "if x%8==1 or x%8==7" is what you see marked red in your text. There is no faster way, this involve a modulus and a selection (pointer). If you do "y=x%8, if y==1 or y==7", you may do the calculus only once, but still do the comparison two times at each loop. So, this loop is very fast, but due to the fact that it tests all 2kp+1 candidates, it is still about 3-4 times slower than the "tf_420c" function which I posted here around, which tests only the candidates in the right classes (which stand a miniscule chance to be prime). Even that function is much slower that other stuff like mfaktc :razz: because it does not do sieving, it does not run multithreaded, and pari is not compiled, but interpreted. edit: Grrrr axn! |
[QUOTE=LaurV;372033]When you check for factors of a number, you only need to check up to square root of the number. The limit is an obfuscated way to write sqrt(2^p-1), or sqrt(1<<p-1), if you like, which might be a bit faster because of the internal pari representation. In fact, its speed depends on the number of decimals, and it could be much faster if you first set default(realprecision,5), get the root and set the realprecision back. The backslash is integer division. Anyhow, the speed of this calculus is not relevant, because it is only executed once in the beginning, and only takes few milliseconds.
The "vectored" part you can understand it better if you write: v=[9,8,7,6]; a=v[2] This will return 8. Now imagine you don't use v, but write directly the constant, as "a=[9,8,7,6][2]", this will still return 8. (indexing starts with 1) The faster way to write "if x%8==1 or x%8==7" is what you see marked red in your text. There is no faster way, this involve a modulus and a selection (pointer). If you do "y=x%8, if y==1 or y==7", you may do the calculus only once, but still do the comparison two times at each loop. So, this loop is very fast, but due to the fact that it tests all 2kp+1 candidates, it is still about 3-4 times slower than the "tf_420c" function which I posted here around, which tests only the candidates in the right classes (which stand a miniscule chance to be prime). Even that function is much slower that other stuff like mfaktc :razz: because it does not do sieving, it does not run multithreaded, and pari is not compiled, but interpreted. edit: Grrrr axn![/QUOTE] Do you know of anyone ever trying something like: [CODE]? a=[1,2,3,4,5]; forstep(x=1,100,v=eval(a), print(x","a); a=vector(#a,n,a[#a-n+1]+a[n%#a+1]) ;v=eval(a) ) 1,[1, 2, 3, 4, 5] 2,[7, 7, 7, 7, 2] 16,[9, 14, 14, 9, 14] 39,[28, 23, 23, 28, 18] 67,[41, 51, 51, 41, 56][/CODE] ? |
| All times are UTC. The time now is 22:21. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.