Results 1 to 7 of 7
Thread: Perrin series
 12042009, 01:57 PM #1Member
 Join Date
 Sep 2009
 Posts
 15
 Rep Power
 0
tough assignment of prime numbers
I have an assignment which made me very confused!!!!:confused:
it says
Java Code:Pirin sequence are similar to Fibonacci series but with the following rule: p(n) = p(n2) + p(n3) ; p(0) = 3 , p(1) = 0 , p(2) = 2 they have a property (now fully proven) that if p(n) is divisible by n then n is prime number (special cases exist). Write a program that will check whether an input x is prime or not using this technique.
and it would be very nice to help me with an algorithm to solve this problem than a code(but code won't hurt :D).Last edited by moamen; 12042009 at 05:41 PM.
 12042009, 03:49 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,438
 Blog Entries
 7
 Rep Power
 20
Is the other way around also true? i.e. if n is prime then p(n) is divisable by n. If so, you have found a linear recurrence relation for the prime numbers ...
kind regards,
Jos (not so optimistic about it ;)
 12042009, 03:59 PM #3Member
 Join Date
 Sep 2009
 Posts
 15
 Rep Power
 0
i guess the vice versa procces is true.
but my problem here is how to make a program that allows me to calculate n!!!!
(e.g) p(11) = 22 ==>22/11 =2 ==> 11 is prime.
my problem here is how to make a program that uses the formula mentiond in the question to calculate n for p(n) (22 for p(11)).
thanks for your help!!:)
 12042009, 04:07 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,438
 Blog Entries
 7
 Rep Power
 20
You can calculate P(n) recursively which is utterly stupid because you are recalculating the same numbers over and over again. Better use an iterative approach and juggle with the intermediate results a bit; the following comes to mind:
Java Code:int P(int n) { int a= 3, b= 0, c= 2; // the first three P(n) switch (n) { case 0: return a; case 1: return b: case 2: return c: default: int m; while (n > 2) { m= a+b; c= m; b= c; a= b; } return m; } }
Jos
 12042009, 05:07 PM #5Member
 Join Date
 Sep 2009
 Posts
 15
 Rep Power
 0
this dosent work ;(
i made another program that gave me the same result. :D
what i need is to make p(11) = #p(0) + #p(1) + #(p2)
// (e.g)p(11) = 4p(0) + 4p(1) + 5p(2) <== this is what i really get after using perrin formula(using a pen and a paper :D) then i substitute p(0) ,p(1) & p(2) with thier values!!!
Thanx very much for your help!!!
 12042009, 05:21 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,438
 Blog Entries
 7
 Rep Power
 20
So I noticed; I should've tested it first; this version does work:
Java Code:public class Perrin { private static int P(int n) { int a = 3, b = 0, c = 2; // the first three P(n) switch (n) { case 0: return a; case 1: return b; case 2: return c; default: int m = 0; while (n > 2) { m = a + b; a = b; b = c; c = m; } return m; } } public static void main(String[] args) { for (int i = 0; i < 10; i++) System.out.println("p(" + i + ")=" + P(i)); } }
Jos
 12042009, 05:27 PM #7Member
 Join Date
 Sep 2009
 Posts
 15
 Rep Power
 0
Similar Threads

Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11232009, 07:37 PM 
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03232009, 11:16 AM 
Getting ExceptionInInitializer exception when i am trying to code a time series chart
By neeraj.singh in forum AWT / SwingReplies: 2Last Post: 02172009, 03:20 PM 
No Fluff Just Stuff Software Symposium Series 2007,
By orchid in forum Reviews / AdvertisingReplies: 0Last Post: 04082007, 08:13 PM
Bookmarks