Author Topic: Recaman Sequence  (Read 2499 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Recaman Sequence
« on: February 18, 2019, 01:17:07 pm »
Stumbled onto this while surfing youtube (see link in code):
Code: QB64: [Select]
  1. _TITLE "Recaman Sequence" 'B+ 2019-02-18
  2.  
  3. 'reference and thanks:
  4. ' [youtube]https://www.youtube.com/watch?v=FGC5TdIiT9U[/youtube]
  5.  
  6. CONST xmax = 1200
  7. CONST ymax = 700
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. _SCREENMOVE 100, 20
  10.  
  11. 'draw a number line spacing integers by SP
  12. CY = ymax / 2
  13. SP = 5 'so 1200/sp = 240 numbers on a numberline
  14. LINE (0, CY)-(xmax, CY)
  15. FOR i = 0 TO xmax / SP
  16.     CIRCLE (i * SP, CY), 1
  17. DIM n(0 TO 16000) 'save places where number is used
  18.  
  19. interval = 1
  20. current = 0
  21. n(current) = -1 'we've been here already!
  22.  
  23.  
  24. ' !!! The rule is to go back if (current position - interval) is not taken and is >= 0, otherwise go forward.
  25. ' The interval is increased by one after each hop.
  26.  
  27.     LINE (0, 0)-(200, 30), _RGB32(0, 0, 0) '  clean out old message
  28.     LOCATE 1, 1: PRINT "Interval:"; interval 'report interval or hop number
  29.     IF LEN(INKEY$) <> 0 THEN q = -1 '         allow bug out any key press
  30.  
  31.     R = interval / 2 'for radius of hop drawing
  32.     'alternate hops with semi circles
  33.     IF interval MOD 2 THEN
  34.         start = _PI: fini = 0 'semi circle start and end over the line
  35.     ELSE
  36.         start = 0: fini = _PI 'semi circle start and end under the line
  37.     END IF
  38.  
  39.     test = current - interval 'test against our Rule
  40.     IF test < 0 THEN
  41.         'draw current to current + interval
  42.         CIRCLE (SP * (current + R), CY), R * SP, , start, fini
  43.         current = current + interval
  44.         n(current) = -1 'we've been here
  45.     ELSEIF n(test) <> 0 THEN
  46.         'draw current to current + interval
  47.         CIRCLE (SP * (current + R), CY), R * SP, , start, fini
  48.         current = current + interval
  49.         n(current) = -1 'we've been here
  50.     ELSE
  51.         'draw back semi current - interval
  52.         CIRCLE (SP * (current - R), CY), R * SP, , start, fini
  53.         current = test
  54.         n(current) = -1 'we've been here
  55.     END IF
  56.     interval = interval + 1
  57.     _DISPLAY
  58.     _LIMIT 1 '<<< however fast you want 1, 10, 100
  59. LOOP UNTIL q OR interval = 3001 'good place to stop for this graph
  60.  
  61. PRINT "What numbers are left after 3000 hops? ":
  62. FOR i = 0 TO xmax / SP
  63.     IF n(i) = 0 THEN PRINT i,
  64. PRINT " the end."
  65.  
  66.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Recaman Sequence
« Reply #1 on: February 18, 2019, 03:00:11 pm »
Nice work, Bplus!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Recaman Sequence
« Reply #2 on: February 18, 2019, 03:04:44 pm »
Thanks Petr, I was thinking about trying sound but have to track down 6 octaves of a Scale.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Recaman Sequence
« Reply #3 on: February 18, 2019, 03:31:05 pm »
maybe - to line 59 insert:

SOUND 50 + interval, .5

for easy sound effect (of course it is not piano :-D)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Recaman Sequence
« Reply #4 on: February 18, 2019, 05:04:12 pm »
In the key of C:
Code: QB64: [Select]
  1. _TITLE "Recaman Sequence" 'B+ 2019-02-18
  2.  
  3. 'reference and thanks:
  4. ' [youtube]https://www.youtube.com/watch?v=FGC5TdIiT9U[/youtube]
  5.  
  6. CONST xmax = 1200
  7. CONST ymax = 700
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. _SCREENMOVE 100, 20
  10.  
  11. 'prep sound array
  12. DIM s(0 TO 41) '6 * 7  mod 42
  13. '      c             d             e             f             g             a             b
  14. s(0) = 65: s(1) = 73: s(2) = 82: s(3) = 87: s(4) = 98: s(5) = 110: s(6) = 123
  15. s(7) = 131: s(8) = 147: s(9) = 165: s(10) = 175: s(11) = 196: s(12) = 220: s(13) = 247
  16. s(14) = 262: s(15) = 294: s(16) = 330: s(17) = 349: s(18) = 392: s(19) = 440: s(20) = 494
  17. s(21) = 523: s(22) = 587: s(23) = 659: s(24) = 698: s(25) = 784: s(26) = 880: s(27) = 988
  18. s(28) = 1047: s(29) = 1175: s(30) = 1318: s(31) = 1397: s(32) = 1568: s(33) = 1760: s(34) = 1976
  19. s(35) = 2093: s(36) = 2349: s(37) = 2637: s(38) = 2794: s(39) = 3136: s(40) = 3520: s(41) = 3951
  20.  
  21. 'draw a number line spacing integers by SP
  22. CY = ymax / 2
  23. SP = 5 'so 1200/sp = 240 numbers on a numberline
  24. LINE (0, CY)-(xmax, CY)
  25. FOR i = 0 TO xmax / SP
  26.     CIRCLE (i * SP, CY), 1
  27. DIM n(0 TO 16000) 'save places where number is used
  28.  
  29. interval = 1
  30. current = 0
  31. n(current) = -1 'we've been here already!
  32.  
  33.  
  34. ' !!! The rule is to go back if (current position - interval) is not taken and is >= 0, otherwise go forward.
  35. ' The interval is increased by one after each hop.
  36.  
  37.     LINE (0, 0)-(200, 30), _RGB32(0, 0, 0) '  clean out old message
  38.     LOCATE 1, 1: PRINT "Interval:"; interval 'report interval or hop number
  39.     IF LEN(INKEY$) <> 0 THEN q = -1 '         allow bug out any key press
  40.  
  41.     R = interval / 2 'for radius of hop drawing
  42.     'alternate hops with semi circles
  43.     IF interval MOD 2 THEN
  44.         start = _PI: fini = 0 'semi circle start and end over the line
  45.     ELSE
  46.         start = 0: fini = _PI 'semi circle start and end under the line
  47.     END IF
  48.  
  49.     test = current - interval 'test against our Rule
  50.     IF test < 0 THEN
  51.         'draw current to current + interval
  52.         CIRCLE (SP * (current + R), CY), R * SP, , start, fini
  53.         current = current + interval
  54.         n(current) = -1 'we've been here
  55.     ELSEIF n(test) <> 0 THEN
  56.         'draw current to current + interval
  57.         CIRCLE (SP * (current + R), CY), R * SP, , start, fini
  58.         current = current + interval
  59.         n(current) = -1 'we've been here
  60.     ELSE
  61.         'draw back semi current - interval
  62.         CIRCLE (SP * (current - R), CY), R * SP, , start, fini
  63.         current = test
  64.         n(current) = -1 'we've been here
  65.     END IF
  66.     SOUND s(current MOD 42), 1
  67.     interval = interval + 1
  68.     _DISPLAY
  69.     _LIMIT 6 '<<< however fast you want 1, 10, 100
  70. LOOP UNTIL q OR interval = 3001 'good place to stop for this graph
  71. FOR i = 6 TO 1 STEP -1
  72.     SOUND s(i * 7 - 6), 2
  73.     _LIMIT 6
  74. SOUND s(16), 1
  75. SOUND s(15), 2
  76. SOUND s(14), 4
  77. SOUND s(16), 8
  78. SOUND s(15), 4
  79. SOUND s(14), 1
  80. SOUND s(14), 8
  81.  
  82. PRINT "What numbers are left after 3000 hops? ":
  83. FOR i = 0 TO xmax / SP
  84.     IF n(i) = 0 THEN PRINT i,
  85. PRINT " the end."
  86.  
  87. '      1* D#1 ...... 39           G3 ....... 196          A#5 ...... 932
  88. '         E1 ....... 41           G#3 ...... 208          B5 ....... 988
  89. '         F1 ....... 44           A3 ....... 220       6* C6 ....... 1047
  90. '         F#1 ...... 46           A#3 ...... 233          C#6 ...... 1109
  91. '         G1 ....... 49           B3 ....... 247          D6 ....... 1175
  92. '         G#1 ...... 51        4* C4 ....... 262          D#6 ...... 1245
  93. '         A1 ....... 55           C#4 ...... 277          E6 ....... 1318
  94. '         A#1 ...... 58           D4 ....... 294          F6 ....... 1397
  95. '         B1 ....... 62           D#4 ...... 311          F#6 ...... 1480
  96. '      2* C2 ....... 65           E4 ....... 330          G6 ....... 1568
  97. '         C#2 ...... 69           F4 ....... 349          G# ....... 1661
  98. '         D2 ....... 73           F#4 ...... 370          A6 ....... 1760
  99. '         D#2 ...... 78           G4 ....... 392          A#6 ...... 1865
  100. '         E2 ....... 82           G#4 ...... 415          B6 ....... 1976
  101. '         F2 ....... 87           A4 ....... 440       7* C7 ....... 2093
  102. '         F#2 ...... 92           A# ....... 466          C#7 ...... 2217
  103. '         G2 ....... 98           B4 ....... 494          D7 ....... 2349
  104. '         G#2 ...... 104       5* C5 ....... 523          D#7 ...... 2489
  105. '         A2 ....... 110          C#5 ...... 554          E7 ....... 2637
  106. '         A#2 ...... 117          D5 ....... 587          F7 ....... 2794
  107. '         B2 ....... 123          D#5 ...... 622          F#7 ...... 2960
  108. '      3* C3 ....... 131          E5 ....... 659          G7 ....... 3136
  109. '         C#3 ...... 139          F5 ....... 698          G#7 ...... 3322
  110. '         D3 ....... 147          F#5 ...... 740          A7 ....... 3520
  111. '         D#3 ...... 156          G5 ....... 784          A#7 ...... 3729
  112. '         E3 ....... 165          G#5 ...... 831          B7 ....... 3951
  113. '         F3 ....... 175          A5 ....... 880       8* C8 ....... 4186
  114. '         F#3 ...... 185
  115.  
  116.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Recaman Sequence
« Reply #5 on: February 18, 2019, 05:15:43 pm »
My goodness! Very beautiful!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Recaman Sequence
« Reply #6 on: February 19, 2019, 12:02:26 pm »
Thanks for your interest [banned user]. It did need a bit of "colour".

Perhaps a 42 color palette to go with the notes, 7 main hues for notes and a shade higher for each octave. Could have done frequencies that way.

math:
base = current mod 7
octave = (current mod 42)\7
sound s(base) * 2 ^ octave





« Last Edit: February 19, 2019, 12:08:30 pm by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
Re: Recaman Sequence
« Reply #7 on: February 21, 2019, 08:54:54 am »
Wow! It look like a amazing fractal to me. :D
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Recaman Sequence
« Reply #8 on: February 21, 2019, 10:04:43 pm »
OK after writing up code for arcRing, I can now draw arcs in alpha at any width without overlap and now can colorize Recaman the way I envisioned plus modified sound as commented above:
Code: QB64: [Select]
  1. _TITLE "Recaman Sequence Sound and Color mod" 'B+ 2019-02-21
  2.  
  3. 'reference and thanks:
  4. ' [youtube]https://www.youtube.com/watch?v=FGC5TdIiT9U[/youtube]
  5.  
  6. CONST xmax = 1200
  7. CONST ymax = 700
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9. _SCREENMOVE 100, 20
  10.  
  11. 'prep sound array
  12. DIM s(0 TO 6) '6 * 7  mod 42
  13. '      c          d          e          f          g          a           b
  14. s(0) = 65: s(1) = 73: s(2) = 82: s(3) = 87: s(4) = 98: s(5) = 110: s(6) = 123
  15. 'prep palette multiples
  16. DIM C(0 TO 6, 0 TO 2)
  17. C(0, 0) = 1: C(0, 1) = 0: C(0, 2) = 0
  18. C(1, 0) = 1: C(1, 1) = 1: C(1, 2) = 0
  19. C(2, 0) = 1: C(2, 1) = 0: C(2, 2) = 1
  20. C(3, 0) = 0: C(3, 1) = 1: C(3, 2) = 0
  21. C(4, 0) = 0: C(4, 1) = 1: C(4, 2) = 1
  22. C(5, 0) = 0: C(5, 1) = 0: C(5, 2) = 1
  23. C(6, 0) = 1: C(6, 1) = 1: C(6, 2) = 1
  24.  
  25. 'draw a number line spacing integers by SP
  26. CY = ymax / 2
  27. SP = 5 'so 1200/sp = 240 numbers on a numberline
  28. 'LINE (0, CY)-(xmax, CY)
  29. 'FOR i = 0 TO xmax / SP
  30. '    CIRCLE (i * SP, CY), 1
  31. 'NEXT
  32. DIM n(0 TO 16000) 'save places where number is used
  33.  
  34. interval = 1
  35. current = 0
  36. n(current) = -1 'we've been here already!
  37. intLimit = 400
  38.  
  39. ' !!! The rule is to go back if (current position - interval) is not taken and is >= 0, otherwise go forward.
  40. ' The interval is increased by one after each hop.
  41.  
  42.  
  43.     LINE (0, 0)-(200, 30), _RGB32(0, 0, 0) '  clean out old message
  44.     COLOR _RGB32(220, 220, 220)
  45.     LOCATE 1, 1: PRINT "Interval:"; interval 'report interval or hop number
  46.     LOCATE 2, 1: PRINT " Current:"; current
  47.     IF LEN(INKEY$) <> 0 THEN q = -1 '         allow bug out any key press
  48.  
  49.     r = interval / 2 'for radius of hop drawing
  50.     'alternate hops with semi circles
  51.     IF interval MOD 2 THEN
  52.         start = _PI: fini = 0 'semi circle start and end over the line
  53.     ELSE
  54.         start = 0: fini = _PI 'semi circle start and end under the line
  55.     END IF
  56.     b = current MOD 7
  57.     octave = INT((current MOD 42) \ 7 + 1)
  58.     SOUND s(b) * 2 ^ octave, 1
  59.     c~& = _RGBA32(C(b, 0) * octave * 30 + 45, C(b, 1) * octave * 30 + 45, C(b, 2) * octave * 30 + 45, 120)
  60.     test = current - interval 'test against our Rule
  61.     IF test < 0 THEN
  62.         'draw current to current + interval
  63.         arcRing SP * (current + r), CY, r * SP, r * SP - SP, start, fini, c~&
  64.         current = current + interval
  65.         n(current) = -1 'we've been here
  66.     ELSEIF n(test) <> 0 THEN
  67.         'draw current to current + interval
  68.         arcRing SP * (current + r), CY, r * SP, r * SP - SP, start, fini, c~&
  69.         current = current + interval
  70.         n(current) = -1 'we've been here
  71.     ELSE
  72.         'draw back semi current - interval
  73.         arcRing SP * (current - r), CY, r * SP, r * SP - SP, start, fini, c~&
  74.         current = test
  75.         n(current) = -1 'we've been here
  76.     END IF
  77.  
  78.     interval = interval + 1
  79.     _DISPLAY
  80.     _LIMIT 6 '<<< however fast you want 1, 10, 100
  81. LOOP UNTIL q OR interval = intLimit + 1 'good place to stop for this graph
  82. FOR i = 6 TO 1 STEP -1
  83.     SOUND s(i) * 2 ^ 3 - 6, 2
  84.     _LIMIT 6
  85. SOUND s(2) * 8, 1
  86. SOUND s(1) * 8, 2
  87. SOUND s(0) * 8, 4
  88. SOUND s(2) * 8, 8
  89. SOUND s(1) * 8, 4
  90. SOUND s(0) * 8, 1
  91. SOUND s(0) * 8, 8
  92.  
  93. COLOR _RGB32(220, 220, 220)
  94. PRINT "What numbers are left after "; intLimit; " hops? ":
  95. FOR i = 0 TO xmax / SP
  96.     IF n(i) = 0 THEN PRINT i,
  97. PRINT " the end."
  98.  
  99. 'doing a circle fill with this runs 20 x's slower than Gold Std Circle Fill
  100. SUB arcRing (x0, y0, outerR, innerR, raStart, raEnd, colr AS _UNSIGNED LONG)
  101.     DIM x AS INTEGER, y AS INTEGER
  102.     PI2 = _PI(2)
  103.     PI32 = _PI(1.5)
  104.     PIh = _PI(.5)
  105.     PI = _PI
  106.     raS = raStart
  107.     WHILE raS >= PI2
  108.         raS = raS - PI2
  109.     WEND
  110.     WHILE raS < 0
  111.         raS = raS + PI2
  112.     WEND
  113.     raE = raEnd
  114.     WHILE raE < 0
  115.         raE = raE + PI2
  116.     WEND
  117.     WHILE raE >= PI2
  118.         raE = raE - PI2
  119.     WEND
  120.     IF raE > raS THEN ck1 = -1
  121.     FOR y = y0 - outerR TO y0 + outerR
  122.         FOR x = x0 - outerR TO x0 + outerR
  123.             dist = SQR((x - x0) * (x - x0) + (y - y0) * (y - y0))
  124.             IF dist >= innerR AND dist <= outerR THEN 'within 2 radii
  125.                 'angle of x, y to x0, y0
  126.                 IF x - x0 <> 0 AND y - y0 <> 0 THEN
  127.                     ra = _ATAN2(y - y0, x - x0)
  128.                     IF ra < 0 THEN ra = ra + PI2
  129.                 ELSEIF x - x0 = 0 THEN
  130.                     IF y >= y0 THEN ra = _PI / 2 ELSE ra = PI32
  131.                 ELSEIF y - y0 = 0 THEN
  132.                     IF x >= x0 THEN ra = 0 ELSE ra = PI
  133.                 END IF
  134.                 IF ck1 THEN 'raEnd > raStart
  135.                     IF ra >= raS AND ra <= raE THEN
  136.                         PSET (x, y), colr
  137.                     END IF
  138.                 ELSE 'raEnd < raStart, raEnd is falls before raStart clockwise so fill through 2 * PI
  139.                     IF ra >= raS AND ra < PI2 THEN
  140.                         PSET (x, y), colr
  141.                     ELSE
  142.                         IF ra >= 0 AND ra <= raE THEN
  143.                             PSET (x, y), colr
  144.                         END IF
  145.                     END IF
  146.                 END IF
  147.             END IF
  148.         NEXT
  149.     NEXT
  150.  
Recaman Seq.PNG
« Last Edit: February 21, 2019, 10:05:55 pm by bplus »