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))) Code: forstep(n=6000,0,1,if(isprime(n) && 6000%n==0, print(n))) Code: for(n=0,6000,if(isprime(n) && 6000%n==0, print(n))) Code: forprime(p=0,6000,if(6000%p==0, print(p))) Code: fordiv(6000,n,if(isprime(n), print(n))) Code: f=factor(6000)[,1]; for(i=1,#f,print(f[i])) * Note: You can also use the singlecharacter 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,122digit 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:
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  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
PARIGP  almahed  Math Software  45  May 19th, 2015 02:30 PM 
Need Help with Pari GP  vittoire  Math Software  3  February 16th, 2014 09:42 AM 
PARI  mathbalarka  Math Software  1  August 17th, 2012 10:50 AM 
Pari  Hoempa  Computer Science  31  March 11th, 2011 07:24 PM 
help on pari/gp  duz  Number Theory  2  March 20th, 2009 05:50 PM 