mersenneforum.org  

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

Reply
 
Thread Tools
Old 2013-07-29, 20:34   #2432
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Cool, I look forward to it!
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
multiply x by 2 (it seems)
Code:
trace(x)
2*x
x<<1
how many that can you the same thing are there ?

Last fiddled with by science_man_88 on 2013-07-29 at 20:34
science_man_88 is offline   Reply With Quote
Old 2013-07-30, 01:07   #2433
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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
multiply x by 2 (it seems)
Code:
trace(x)
2*x
x<<1
how many that can you the same thing are there ?
Lots of ways. But be careful, these aren't quite equivalent. trace(I) == 0, for example.
CRGreathouse is offline   Reply With Quote
Old 2013-08-04, 02:06   #2434
ismillo
 
Jul 2013
Brazil

19 Posts
Default

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)
}
v2i1d(x) is CRG function for vectors to integer with one digit each position.

Code:
v2i1d(a)=subst(Pol(a), 'x, 10)
It has some problems, however, I think it will handle ok.
ismillo is offline   Reply With Quote
Old 2013-08-04, 02:23   #2435
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

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)
};
CRGreathouse is offline   Reply With Quote
Old 2013-08-04, 12:42   #2436
ismillo
 
Jul 2013
Brazil

19 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
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)
};
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))
}.;
ismillo is offline   Reply With Quote
Old 2014-02-15, 16:12   #2437
vittoire
 
Feb 2014

216 Posts
Default 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?
vittoire is offline   Reply With Quote
Old 2014-02-16, 04:56   #2438
vittoire
 
Feb 2014

2 Posts
Default 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.
vittoire is offline   Reply With Quote
Old 2014-04-26, 01:24   #2439
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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,sqrt(2)<<(p\2),2*p,
    [1,0,0,0,0,0,1][q%8] && Mod(2, q)^p==1 && return(q)
  );
  1<<p-1
};
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.

Last fiddled with by science_man_88 on 2014-04-26 at 01:54
science_man_88 is offline   Reply With Quote
Old 2014-04-26, 03:26   #2440
axn
 
axn's Avatar
 
Jun 2003

32·5·113 Posts
Default

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
axn is offline   Reply With Quote
Old 2014-04-26, 04:15   #2441
LaurV
Romulan Interpreter
 
LaurV's Avatar
 
Jun 2011
Thailand

3·3,221 Posts
Default

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 because it does not do sieving, it does not run multithreaded, and pari is not compiled, but interpreted.

edit: Grrrr axn!

Last fiddled with by LaurV on 2014-04-26 at 04:19
LaurV is offline   Reply With Quote
Old 2014-04-26, 18:53   #2442
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by LaurV View Post
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 because it does not do sieving, it does not run multithreaded, and pari is not compiled, but interpreted.

edit: Grrrr axn!
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]
?
science_man_88 is offline   Reply With Quote
Reply



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 06:55.


Fri Aug 6 06:55:29 UTC 2021 up 14 days, 1:24, 1 user, load averages: 2.49, 2.64, 2.71

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.