My Math Forum Conditional in PARI
 User Name Remember Me? Password

 Computer Science Computer Science Forum

 June 23rd, 2010, 02:22 PM #1 Global Moderator     Joined: Oct 2008 From: London, Ontario, Canada - The Forest City Posts: 7,968 Thanks: 1152 Math Focus: Elementary mathematics and beyond Conditional in PARI How would I code the following in PARI? Code: for(n=6000,0,if(isprime(n) and 6000/n has remainder 0, print(n))
 June 23rd, 2010, 02:33 PM #2 Global Moderator     Joined: Nov 2006 From: UTC -5 Posts: 16,046 Thanks: 938 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms Re: Conditional in PARI "And" is &&, "or" is ||, and modular division is %:* Code: for(n=6000,0,if(isprime(n) && 6000%n==0, print(n))) But this code won't do anything, because there are no numbers n with 6000 <= n <= 0. If you want to loop backward, specify a step of -1: Code: forstep(n=6000,0,-1,if(isprime(n) && 6000%n==0, print(n))) If you don't need to go backward, forward would just be Code: for(n=0,6000,if(isprime(n) && 6000%n==0, print(n))) But in this case, you would get faster code if you looped through only the primes rather than testing for primality each time. I prefer to use p (and q) for prime loop variables, so I'll also change n to p -- but that's just personal taste. Code: forprime(p=0,6000,if(6000%p==0, print(p))) Of course 6000 mod a prime can be 0 only when the prime divides 6000, so you could go even faster with Code: fordiv(6000,n,if(isprime(n), print(n))) You can improve the algorithm (at the cost of some readability) by not looking at the composite divisors at all. First factor the number, then take the first component ("[,1]") of the factors (the primes themselves -- the second component is the exponents). Then loop through this list, printing out each factor: Code: f=factor(6000)[,1]; for(i=1,#f,print(f[i])) * Note: You can also use the single-character forms & and | for "and" and "or". Sometimes rather than using modular division on integers directly you should use an intmod. (3^1000000)%10 calculates a 477,122-digit number then finds the remainder mod 10; Mod(3,10)^1000000 takes 3 mod 10 and raises it to the power of a million, reducing at each step so the number never gets large. To recover an integer from an intmod, use lift() or centerlift(), the latter choosing the number with the least absolute value.
 June 23rd, 2010, 02:37 PM #3 Global Moderator     Joined: Oct 2008 From: London, Ontario, Canada - The Forest City Posts: 7,968 Thanks: 1152 Math Focus: Elementary mathematics and beyond Re: Conditional in PARI Thank you!
 June 23rd, 2010, 02:43 PM #4 Global Moderator     Joined: Nov 2006 From: UTC -5 Posts: 16,046 Thanks: 938 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms Re: Conditional in PARI Something of an overcomplete answer, I imagine. But I thought it would be better to say more than less in this case. I edited in a note about modular arithmetic.
 June 25th, 2010, 04:07 AM #5 Global Moderator     Joined: Oct 2008 From: London, Ontario, Canada - The Forest City Posts: 7,968 Thanks: 1152 Math Focus: Elementary mathematics and beyond Re: Conditional in PARI Your thinking is correct. I am new to PARI (and number theory), so I found your reply quite helpful.
June 25th, 2010, 06:43 AM   #6
Global Moderator

Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 938

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Conditional in PARI

Quote:
 Originally Posted by greg1313 Your thinking is correct. I am new to PARI (and number theory), so I found your reply quite helpful.
Glad to hear it. Feel free to ask more questions as the need arises.

I should point out (in case you missed it in my wall of text above) that the syntax of fordiv is not the same as for/forstep/forprime:
Code:
for(n=1,10,print(n))
forstep(n=1,10,2,print(n))
forprime(n=1,10,print(n))
fordiv(10,n,print(n))

 Tags conditional, pari

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post al-mahed Math Software 45 May 19th, 2015 02:30 PM vittoire Math Software 3 February 16th, 2014 09:42 AM mathbalarka Math Software 1 August 17th, 2012 10:50 AM Hoempa Computer Science 31 March 11th, 2011 07:24 PM duz Number Theory 2 March 20th, 2009 05:50 PM

 Contact - Home - Forums - Cryptocurrency Forum - Top