Author Topic: Little break to visualize prime numbers in a million real numbers.  (Read 4657 times)

0 Members and 1 Guest are viewing this topic.

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Thought I'd take a little break from "storm chasing" to wonder in the visualization of prime numbers. :)

WORKING CODE uploaded below.

Code: QB64: [Select]
  1. _TITLE "The Patterns in Primes"
  2.  
  3. CONST WDTH = 999
  4. CONST HGHT = 999
  5.  
  6. SCREEN _NEWIMAGE(WDTH, HGHT, 32)
  7.  
  8. DEFLNG A-Z
  9.  
  10. Limit = WDTH * HGHT
  11.  
  12. DIM Flags(Limit) AS LONG
  13.  
  14. Flags(0) = 0
  15. Flags(1) = 0
  16. FOR N = 2 TO SQR(Limit)
  17.     IF Flags(N) = 0 THEN
  18.         FOR K = N * N TO Limit STEP N
  19.             Flags(K) = 1
  20.         NEXT K
  21.     END IF
  22.  
  23. ' Display the primes
  24.  
  25. 'Spiral________________________________________________________________________
  26. 'N = 2
  27. 'E = UBOUND(Flags)
  28. 'Radius! = 0
  29. 'HalfWdth = WDTH / 2
  30. 'HalfHght = HGHT / 2
  31.  
  32. 'FOR i# = 0 TO Limit STEP .007
  33. '    IF Flags(N) = 0 THEN
  34. '        PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#)), _RGB(0, 160, 0)
  35. '    ELSE
  36. '        PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#)), _RGB(0, 0, 0)
  37. '    END IF
  38. '    IF N = E THEN EXIT FOR
  39. '    N = N + 1
  40. '    'PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#))
  41. '    Radius# = Radius# + .0007
  42. 'NEXT i#
  43.  
  44. 'L to R, T to B________________________________________________________________
  45. E = UBOUND(Flags)
  46. FOR X = 0 TO HGHT
  47.     FOR Y = 0 TO WDTH
  48.         IF Flags(N) = 0 THEN
  49.             PSET (Y, X), _RGB(0, 160, 0)
  50.         ELSE
  51.             PSET (Y, X), _RGB(0, 0, 0)
  52.         END IF
  53.         IF N = E THEN EXIT FOR
  54.         N = N + 1
  55.     NEXT
  56.  
  57.  
  58.  
PrimesHorizontal.png
* PrimesHorizontal.png (Filesize: 100.62 KB, Dimensions: 1001x1026, Views: 139)
PrimesSpiral.png
* PrimesSpiral.png (Filesize: 81.87 KB, Dimensions: 1001x1026, Views: 125)
« Last Edit: February 06, 2022, 01:19:29 pm by Phlashlite »

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #1 on: February 06, 2022, 12:50:46 pm »
Hmmm.... now that I start looking closely I'm not certain this is actually working correctly...
not sure.png
* not sure.png (Filesize: 494.83 KB, Dimensions: 973x788, Views: 135)

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #2 on: February 06, 2022, 01:10:42 pm »
I think I can see Agent Smith.

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #3 on: February 06, 2022, 01:18:51 pm »
OK... this one works :)

It's interesting.  with WDTH and HGHT of 999... all the "4,5,6" line up and put clear vertical stripes  of non primes in the image.

Code: QB64: [Select]
  1. '$DEBUG
  2. _TITLE "The Patterns in Primes"
  3.  
  4. CONST WDTH = 1000
  5. CONST HGHT = 1000
  6.  
  7. SCREEN _NEWIMAGE(WDTH, HGHT, 32)
  8.  
  9. DEFLNG A-Z
  10.  
  11. Limit = WDTH * HGHT
  12.  
  13. DIM Flags(Limit) AS LONG
  14.  
  15.  
  16. Flags(0) = 1
  17. Flags(1) = 1
  18. Flags(2) = 0
  19. Flags(3) = 0
  20. FOR N = 2 TO SQR(Limit)
  21.     IF Flags(N) = 0 THEN
  22.         FOR K = N * N TO Limit STEP N
  23.             Flags(K) = 1
  24.         NEXT K
  25.     END IF
  26.  
  27. ' Display the primes
  28.  
  29. 'Spiral________________________________________________________________________
  30. 'N = 2
  31. 'E = UBOUND(Flags)
  32. 'Radius! = 0
  33. 'HalfWdth = WDTH / 2
  34. 'HalfHght = HGHT / 2
  35.  
  36. 'FOR i# = 0 TO Limit STEP .007
  37. '    IF Flags(N) = 0 THEN
  38. '        PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#)), _RGB(0, 160, 0)
  39. '    ELSE
  40. '        PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#)), _RGB(0, 0, 0)
  41. '    END IF
  42. '    IF N = E THEN EXIT FOR
  43. '    N = N + 1
  44. '    'PSET (HalfWdth + Radius# * COS(i#), HalfHght - Radius# * SIN(i#))
  45. '    Radius# = Radius# + .0007
  46. 'NEXT i#
  47.  
  48. 'L to R, T to B________________________________________________________________
  49. N = 0
  50. E = UBOUND(Flags)
  51. FOR X = 0 TO HGHT
  52.     FOR Y = 0 TO WDTH
  53.         IF Flags(N) = 0 THEN
  54.             PSET (Y, X), _RGB(0, 160, 0)
  55.         ELSE
  56.             PSET (Y, X), _RGB(0, 0, 0)
  57.         END IF
  58.         IF N = E THEN EXIT FOR
  59.         N = N + 1
  60.     NEXT
  61.  
  62.  
  63.  
« Last Edit: February 06, 2022, 01:31:59 pm by Phlashlite »

Offline Phlashlite

  • Newbie
  • Posts: 50
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #4 on: February 06, 2022, 01:23:32 pm »
I think I can see Agent Smith.

That's a different program :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #5 on: February 06, 2022, 09:42:23 pm »
Prime spiral with zoom in / out and number ID at mouse point:
Code: QB64: [Select]
  1. _Title "Primes Blue, Composites Red:    i to focus in,    o to focus out"
  2. 'ashish copy 2018-01-08
  3.  
  4. 'bplus mods:
  5. '2018-01-08  mod of isPrime%% speeds up graphic
  6. '2018-01-09  now with focus in and out!
  7.  
  8.  
  9. w = 600
  10. Screen _NewImage(w, w, 32)
  11. Dim numData(w, w) As _Unsigned Integer
  12.  
  13. ox = w / 2
  14. oy = w / 2
  15. limit = w / 3
  16. xx = ox
  17. yy = oy
  18. path = 1
  19. direction = 1
  20. numData(ox, oy) = 0
  21.     Select Case direction
  22.         Case 1 'down
  23.             For i = 1 To path
  24.                 yy = yy + 1
  25.                 n = n + 1
  26.                 numData(xx, yy) = n
  27.             Next
  28.         Case 2 'right
  29.             For i = 1 To path
  30.                 xx = xx + 1
  31.                 n = n + 1
  32.                 numData(xx, yy) = n
  33.             Next
  34.             path = path + 1
  35.         Case 3 'up
  36.             For i = 1 To path
  37.                 yy = yy - 1
  38.                 n = n + 1
  39.                 numData(xx, yy) = n
  40.             Next
  41.         Case 4 'left
  42.             For i = 1 To path
  43.                 xx = xx - 1
  44.                 n = n + 1
  45.                 numData(xx, yy) = n
  46.             Next
  47.             path = path + 1
  48.     End Select
  49.     direction = direction + 1
  50.     If direction > 4 Then direction = 1
  51. Loop Until path > w
  52.  
  53.  
  54. sq = 3
  55. Dim vs(path, path) As _Unsigned Integer
  56. Color _RGB(255, 255, 255)
  57.     yy = 0
  58.     For y = Int(oy - path / (2 * sq) + 1) To Int(oy + path / (2 * sq) - 1)
  59.         yy = yy + 1: xx = 0
  60.         For x = Int(ox - path / (2 * sq) + 1) To Int(ox + path / (2 * sq) - 1)
  61.             xx = xx + 1
  62.             For vy = 0 To sq
  63.                 If yy * sq - sq + vy >= 0 And yy * sq - sq + vy < path Then
  64.                     For vx = 0 To sq
  65.                         If xx * sq - sq + vx >= 0 And xx * sq - sq + vx < path Then
  66.                             vs(xx * sq - sq + vx, yy * sq - sq + vy) = numData(x, y)
  67.                         End If
  68.                     Next
  69.                 End If
  70.             Next
  71.             'LINE (xx * sq - 1, yy * sq - 1)-STEP(sq - 1, sq - 1), C&, BF
  72.         Next
  73.     Next
  74.     Cls
  75.     Do
  76.         K$ = InKey$
  77.         If K$ = Chr$(27) Then
  78.             End 'escape clause
  79.         ElseIf K$ = "i" Then
  80.             If sq + 1 < 50 Then
  81.                 sq = sq + 1
  82.                 Exit Do
  83.             End If
  84.         ElseIf K$ = "o" Then
  85.             If sq - 1 >= 3 Then
  86.                 sq = sq - 3
  87.                 Exit Do
  88.             End If
  89.         End If
  90.         Cls
  91.         yy = 0
  92.         For y = Int(oy - path / (2 * sq) + 1) To Int(oy + path / (2 * sq) - 1)
  93.             yy = yy + 1: xx = 0
  94.             For x = Int(ox - path / (2 * sq) + 1) To Int(ox + path / (2 * sq) - 1)
  95.                 xx = xx + 1
  96.                 If isPrime(numData(x, y)) Then C& = _RGB(0, 0, 255) Else C& = _RGB(255, 0, 0)
  97.                 Line (xx * sq - sq, yy * sq - sq)-Step(sq - 2, sq - 2), C&, BF
  98.                 If x = ox And y = oy Then
  99.                     Line (xx * sq - sq, yy * sq - sq)-Step(sq - 2, sq - 2), _RGB(255, 255, 0), BF
  100.                 End If
  101.             Next
  102.         Next
  103.  
  104.         While _MouseInput: Wend
  105.         mouseX = _MouseX
  106.         mouseY = _MouseY
  107.  
  108.         If mouseX >= 0 And mouseY >= 0 And mouseX <= path And mouseY <= path Then
  109.             t$ = LTrim$(Str$(vs(mouseX, mouseY)))
  110.             If mouseX < 100 Then
  111.                 xAdj = 10
  112.             ElseIf mouseX > w - 100 Then
  113.                 xAdj = -Len(t$) * 8 - 10
  114.             Else
  115.                 xAdj = -Len(t$) * 4
  116.             End If
  117.             If mouseY <= w / 2 Then yAdj = 20 Else yAdj = -20
  118.  
  119.             _PrintString (mouseX + xAdj, mouseY + yAdj), t$
  120.             _Display
  121.         End If
  122.         _Display
  123.         _Limit 60
  124.     Loop
  125.  
  126. Function isPrime%% (num)
  127.     If num Mod 2 = 0 Then
  128.         If num = 2 Then isPrime%% = -1
  129.         Exit Function
  130.     Else
  131.         If num = 1 Then
  132.             Exit Function
  133.         Else
  134.             For i = 3 To Sqr(num) Step 2
  135.                 If num Mod i = 0 Then Exit Function
  136.             Next
  137.             isPrime%% = -1
  138.         End If
  139.     End If
  140.  
  141.  

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #6 on: February 06, 2022, 09:45:16 pm »
Prime spiral with zoom in / out and number ID at mouse point:

is this a Golden Spiral?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #7 on: February 06, 2022, 09:46:34 pm »
Heck no, it a simple little square spiral by a deranged spider :-))

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Little break to visualize prime numbers in a million real numbers.
« Reply #8 on: February 07, 2022, 03:03:22 am »
"I'm seeing spots before my eyes!"
"Have you seen a Doctor?"
"No. Just spots..."
Logic is the beginning of wisdom.