Advertisement
-Kasai-

Recamán sequence generator in Python 3

Jun 19th, 2018
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #Importing time so you can see how long generation took
  2. import time
  3.  
  4. #Screen size finder shamelessly stolen from StackOverflow
  5. import ctypes
  6. user32=ctypes.windll.user32
  7. width=user32.GetSystemMetrics(0)
  8. height=user32.GetSystemMetrics(1)
  9. middle=height/2
  10.  
  11. #Preparing variables for sequence generator
  12. #limit is how far the sequence is generated for
  13. print("Recaman sequence generator by u/-Kasai-")
  14. limit=int(input("How many numbers do you want generated? "))
  15. sequence=[0]
  16. location=0
  17.  
  18. #Generator loop
  19. print("Generating sequence...")
  20. start_time=time.time()
  21. for i in range(1,limit):
  22.     if location-i>0 and location-i not in sequence:
  23.         location-=i
  24.         sequence.append(location)
  25.     else:
  26.         location+=i
  27.         sequence.append(location)
  28. #print(sequence)
  29. #^ You can delete the hashtag above and see the sequence in the shell
  30.  
  31. #Zoom dictates the pixel to number ratio
  32. #Zoom is auto-fit to sequence limit. If you wanted, you could continue the sequence for a true image, but things get a bit messy
  33. #Originally zoom was based off the size of the sequence. It's now set to just fit the sequence from end to end
  34. zoom=width/max(sequence)
  35.  
  36. #Drawing the tkinter canvas
  37. print("Drawing...")
  38. from tkinter import *
  39. Tk=Tk()
  40. Screen=Canvas(Tk,width=width,height=height)
  41. #Adding white background
  42. Screen.create_rectangle(0,0,width,height,fill='#ffffff')
  43. Screen.pack()
  44. #This function draws the semicircles that make up the pattern
  45. def semicircle(start,end,direction):
  46.     if direction=='up':
  47.         starting=0
  48.     elif direction=='down':
  49.         starting=180
  50.     #The middle variable here is half the screen height, as defined earlier. Changing the middle variable will shift the sequence up or down vertically
  51.     Screen.create_arc(start,middle-abs((start-end)/2),end,middle+abs((start-end)/2),style=ARC,start=starting,extent=180)
  52.  
  53. #This variable dictates whether the first step will go up or down. Numberphile goes down. Change it to 'up' if you want!
  54. direct='down'
  55. #This loop draws the pattern
  56. for i in range(len(sequence)-1):
  57.     semicircle(sequence[i]*zoom,sequence[i+1]*zoom,direct)
  58.     if direct=='up':
  59.         direct='down'
  60.     else:
  61.         direct='up'
  62.  
  63. #Finding the ending time
  64. end_time=time.time()
  65.  
  66. print("Done!")
  67. print("Generated and drawn in", round(end_time-start_time,4), "seconds.")
  68. print("Drawing should be in another window.")
  69. print("\nProgram created by u/-Kasai-")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement