Ooops
March 05, 2021, 07:36:10 AM

Author Topic: [bmx] Number sequences and special ratios by plash [ 1+ years ago ]  (Read 474 times)

Offline BlitzBot

Title : Number sequences and special ratios
Author : plash
Posted : 1+ years ago

Description : Example code:
Code: [Select]
SuperStrict

Framework brl.blitz

Import brl.standardio
'Import brl.math

Include "sequences.bmx"

Local processcount:Int = 32, time:Int
Local output:String, elements:Long[], element:Long

Local test_names:String[] = ["Lucas Sequence","Fibonacci Sequence","Perrin Sequence","Pell Sequence","Padovan Sequence"]
Local test_pointers:Long[](count:Int)[] = [LucasSequence, FibonacciSequence, PerrinSequence, PellSequence, PadovanSequence]

For Local index:Int = 0 Until test_pointers.Length
output = Null
time = MilliSecs()
elements = test_pointers[index](processcount)
For element = EachIn elements
output:+ element + " "
Next
Print(test_names[index] + "[" + processcount + "] {" + String(MilliSecs() - time) + "ms}: " + output[..output.Length - 1])

output = Null; time = MilliSecs()
For element = EachIn elements
If IsPrime(element)
output:+ element + " "
End If
Next
Print("Primes {" + String(MilliSecs() - time) + "ms}: " + output[..output.Length - 1] + "~n")
Next


Code :
Code: BlitzMax
  1. Rem
  2. Description: Some number sequence functions (Lucas, Fibonacci, Perrin, Pell and Padovan), a fast IsPrime function
  3.                         and the Golden and Silver ratios.
  4. Author: Plash
  5. Credits: Toby Herring for the IsPrime function (see function header down yonder)
  6.                 All other formulae is taken from Wikipedia.
  7. End Rem
  8.  
  9. ' (Phi) The Golden Ratio; "extreme and mean ratio"
  10. ' http://en.wikipedia.org/wiki/Golden_ratio
  11. Global golden_ratio:Double = 1.6180339887498949         ' Algorithm: (1 + Sqr(5)) / 2
  12.  
  13. ' (DeltaS) The Silver Ratio
  14. ' http://en.wikipedia.org/wiki/Silver_ratio
  15. Global silver_ratio:Double = 2.4142135623730949         ' Algorithm: (1 + Sqr(2))
  16. ' The inverse (?) of the Silver Ratio (used in the closed form Pell numbers formula)
  17. Global isilver_ratio:Double = -0.41421356237309503      ' Algorithm: (1 - Sqr(2))
  18.  
  19.  
  20. ' Fast prime-finder algorithm by Toby Herring; converted and adopted from:
  21. ' http://www.freevbcode.com/ShowCode.asp?ID=1059
  22. Function IsPrime:Int(testprime:Long)
  23.         ' Going by the Wiki prime number list, and eliminating even numbers
  24.         If (testprime < 2) Or (testprime Mod 2) = 0 Then Return False Else If testprime = 2 Then Return True
  25.        
  26.         ' Loop through odd numbers starting with 3
  27.         Local testnum:Long = 3
  28.         Local testlimit:Long = testprime
  29.         While (testlimit > testnum)
  30.                 If (testprime Mod testnum) = 0    
  31.                         Return False
  32.                 End If
  33.                 testlimit = testprime / testnum ' There's logic to this. Think about it.
  34.                 testnum:+ 2 ' Only check odd numbers
  35.         End While
  36.         Return True
  37. End Function
  38.  
  39. ' The Lucas numbers (http://en.wikipedia.org/wiki/Lucas_number)
  40. Function LucasSequence:Long[](count:Int)
  41.         If count = 0 Then Return Null
  42.         Local L:Long[] = New Long[count]
  43.         For Local n:Int = 0 Until count
  44.                 If n = 0
  45.                         L[n] = 2
  46.                 Else If n = 1
  47.                         L[n] = 1
  48.                 Else
  49.                         L[n] = L[n - 1] + L[n - 2]
  50.                         ' Or L[n] = (golden_ratio^n) + ((1 - golden_ratio)^n)
  51.                 End If
  52.         Next
  53.         Return L
  54. End Function
  55.  
  56. ' The Fibonacci numbers (http://en.wikipedia.org/wiki/Fibonacci_number)
  57. Function FibonacciSequence:Long[](count:Int)
  58.         If count = 0 Then Return Null
  59.         Local F:Long[] = New Long[count]
  60.         For Local n:Int = 0 Until count
  61.                 If n < 2
  62.                         F[n] = n ' F[0] = 0; F[1] = 1
  63.                 Else
  64.                         F[n] = F[n - 1] + F[n - 2]
  65.                 End If
  66.         Next
  67.         Return F
  68. End Function
  69.  
  70. ' The Perrin numbers (http://en.wikipedia.org/wiki/Perrin_number)
  71. Function PerrinSequence:Long[](count:Int)
  72.         If count = 0 Then Return Null
  73.         Local P:Long[] = New Long[count]
  74.         For Local n:Int = 0 Until count
  75.                 ' P[0] = 3, P[1] = 0, P[2] = 2
  76.                 If n = 0
  77.                         P[n] = 3
  78.                 Else If n = 1
  79.                         P[n] = 0
  80.                 Else If n = 2
  81.                         P[n] = 2
  82.                 Else
  83.                         P[n] = P[n - 2] + P[n - 3]
  84.                 End If
  85.         Next
  86.         Return P
  87. End Function
  88.  
  89. ' The Pell numbers (http://en.wikipedia.org/wiki/Pell_number)
  90. Function PellSequence:Long[](count:Int)
  91.         If count = 0 Then Return Null
  92.         Local P:Long[] = New Long[count]
  93.         For Local n:Int = 0 Until count
  94.                 ' P[0] = 0, P[1] = 1
  95.                 If n < 2
  96.                         P[n] = n
  97.                 Else
  98.                         P[n] = 2 * (P[n - 1]) + P[n - 2]
  99.                         ' Or P[n] = ((silver_ratio^n) - (isilver_ratio^n)) / 2^2
  100.                 End If
  101.         Next
  102.         Return P
  103. End Function
  104.  
  105. ' The Padovan sequence (http://en.wikipedia.org/wiki/Padovan_sequence)
  106. Function PadovanSequence:Long[](count:Int)
  107.         If count = 0 Then Return Null
  108.         Local P:Long[] = New Long[count]
  109.         For Local n:Int = 0 Until count
  110.                 If n < 3
  111.                         P[n] = 1 ' P[0 to 2] = 1
  112.                 Else
  113.                         P[n] = P[n - 2] + P[n - 3]
  114.                 End If
  115.         Next
  116.         Return P
  117. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal