Recaman.frink

Download or view Recaman.frink in plain text format

/** Solver for Rosetta Code problem "Recaman's sequence"

    https://rosettacode.org/wiki/Recaman's_sequence
*/


class Recaman
{
   var current = 0
   var stepSize = 1
   var seen = new set[0]

   getNext[] :=
   {
      ret = current
      nextVal = current - stepSize
      if nextVal < 0 or seen.contains[nextVal]
         nextVal = current + stepSize
      seen.put[nextVal]
      stepSize = stepSize + 1
      current = nextVal
      return ret
   }

   draw[num] :=
   {
      arc = 1   // Multiplier indicating up or down
      gp = new GeneralPath
      gp.moveTo[current, 0]
      p1 = current
      for n = 1 to num
      {
         p2 = getNext[]
         c = p1 + (p2-p1)/2
         gp.circularArc[c, 0, 180 deg arc signum[p2-p1]]
         p1 = p2
         arc = -arc
      }

      g = new graphics
      g.stroke[0.01]
      g.add[gp]
      return g
   }
}

r = new Recaman
for n = 1 to 15
   print[r.getNext[] + " "]
println[]

r = new Recaman
g = r.draw[400]
g.show[.99]
//g.printTiled[2,1]


Download or view Recaman.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen, eliasen@mindspring.com