Message Boards Message Boards

0
|
3350 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Calculate John Conway's Subprime Fibs with a RecurrenceTable?

Posted 8 years ago

I tried to code John Conway's Subprime Fibs into Mathematica. This is a sequence where each number is the sum of the two previous number, divided by the smallest factor when this sum isn't prime. In the code below everything works correctly except for the recurrence table. The third entry should be 5. Is it me? You can tell me if it is :-)

In[25]:= SD[x_] :=(*smallest divisor not equal to x*)
 If[Or[x == 1, PrimeQ[x]], 1, Divisors[x][[2]]]

SS[x_] := (* x if prime, otherwise x/ smallest prime factor of x *)
   x/SD[x]

In[27]:= S[x_, y_] := 
 SS[x + y] (* x+y devided by it's smallest factor, or itself if prime*)


RecurrenceTable[{a[n + 2] == S[a[n + 1], a[n]], a[1] == 2, 
  a[2] == 3}, a, {n, 10}]

Out[28]= {2, 3, 1, 2, 1, 1, 1, 1, 1, 1}

In[29]:= SD[5]

Out[29]= 1

In[30]:= SS[5]

Out[30]= 5

In[31]:= S[3, 2]

Out[31]= 5
POSTED BY: stephen Morris

The problem is in PrimeQ[] PrimeQ[a] when a is symbol (or anything that is not a prime) immediately returns False, although if its argument is a[n+1]+a[n] which is a symbolic expression. Therefore your SD function return for the first iteration the value 5 (Divisors[5] returns {1,5} so Divisors[5][[2]] is 5) and the result of the first element is 1 What you need to do is to replace SD with another function that will not return immediately a value when a symbolic expression is given as its input option 1

SD[x_] :=(*smallest divisor not equal to x*) 
  If[Length[Divisors[x]] <= 2, 1, Divisors[x][[2]]];
SS[x_] :=(*x if prime,otherwise x/smallest prime factor of x*)
  x/SD[x];
S[x_, y_] := 
 SS[x + y] (*x+y devided by it's smallest factor,or itself if prime*)
\
RecurrenceTable[{a[n + 2] == S[a[n + 1], a[n]], a[1] == 2, 
  a[2] == 3}, a, {n, 10}]

and option 2

SD[x_Integer] :=(*smallest divisor not equal to x*) 
  If[x == 1 || PrimeQ[x], 1, Divisors[x][[2]]];
SS[x_] :=(*x if prime,otherwise x/smallest prime factor of x*)
  x/SD[x];
S[x_, y_] := 
 SS[x + y] (*x+y devided by it's smallest factor,or itself if prime*)
\
RecurrenceTable[{a[n + 2] == S[a[n + 1], a[n]], a[1] == 2, 
  a[2] == 3}, a, {n, 10}]

Notice that in both cases only SD is changed (since the problem is with PrimeQ In person, I prefer the second solution best yehuda

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract