The first thing I came across was how to construct a hexagon in R. After I figured out how to define one and how to plot it, the next idea was to animate it. And this is the source code:
# Hexagon ---------------------------------------------------------------- # Activate plot window prior to start # Aspect ratio can be set using xlim # Limit for loop counter: -G thru +G: G = 240 # Loop counter: i = -G; # Setting direction to upwards: ri = 1; while(i == i) { a=-i; b=i; U <- c(cos((a+90)*2*pi/360), cos((a+30)*2*pi/360), cos((a+330)*2*pi/360), cos((a+270)*2*pi/360), cos((a+210)*2*pi/360), cos((a+150)*2*pi/360), cos((a+90)*2*pi/360)); V <- c(sin((b+90)*2*pi/360), sin((b+30)*2*pi/360), sin((b+330)*2*pi/360), sin((b+270)*2*pi/360), sin((b+210)*2*pi/360), sin((b+150)*2*pi/360), sin((b+90)*2*pi/360)); # Try 0.1s first, for fast computers 0.04 or 0.05: Sys.sleep(.06); plot(U, V, xlim = c(-10, 10), ylim = c(-2, 2), pin = c(5, 5), type = "l"); if(ri == 1){ # counterclockwise: i = i + 20; } else { # clockwise: i = i - 3; } # switch direction: if(i >= G){ ri = 0; } if(i <= -G){ ri = 1; } print(i); }
With the correct setting of the start values and defining U and V the plot command creates this plot of a basic hexagon:
Running the whole code section brings it to life. The sys.sleep command is essential, you have to experiment with the value in order to adapt the program to your computer.
This Python program outputs a Collatz sequence for a given number:
# Calculate Collatz sequence import sys from math import * def collatz(n): if n%2==0: return n//2 else: return 3*n+1 def collatz_seq(s): global z z=0 while True: z=z+1 print(s, end=" ") if s==1: return s=collatz(s) def inputafterstart(): global n n=int(input("Enter an integer greater 1: ")) ###=========================### ### Program starting point! ### ###=========================### if len(sys.argv)-1==0: # We got a commandline argument: inputafterstart() else: n = int(sys.argv[1]) print("Calculating with", n, "as argument!") collatz_seq(n) print("\nReady after",z,"loops.")
The next step was to create a table containing all the values of a Collatz sequence. The table looks like this:
run,value 146,9870987 145,29612962 144,14806481 143,44419444 142,22209722 141,11104861 140,33314584 139,16657292 138,8328646 137,4164323 136,12492970 135,6246485 134,18739456 133,9369728 132,4684864 131,2342432 130,1171216 129,585608 128,292804 127,146402 126,73201 125,219604 124,109802 123,54901 122,164704 121,82352 120,41176 119,20588 118,10294 117,5147 116,15442 115,7721 114,23164 113,11582 112,5791 111,17374 110,8687 109,26062 108,13031 107,39094 106,19547 105,58642 104,29321 103,87964 102,43982 101,21991 100,65974 99,32987 98,98962 97,49481 96,148444 95,74222 94,37111 93,111334 92,55667 91,167002 90,83501 89,250504 88,125252 87,62626 86,31313 85,93940 84,46970 83,23485 82,70456 81,35228 80,17614 79,8807 78,26422 77,13211 76,39634 75,19817 74,59452 73,29726 72,14863 71,44590 70,22295 69,66886 68,33443 67,100330 66,50165 65,150496 64,75248 63,37624 62,18812 61,9406 60,4703 59,14110 58,7055 57,21166 56,10583 55,31750 54,15875 53,47626 52,23813 51,71440 50,35720 49,17860 48,8930 47,4465 46,13396 45,6698 44,3349 43,10048 42,5024 41,2512 40,1256 39,628 38,314 37,157 36,472 35,236 34,118 33,59 32,178 31,89 30,268 29,134 28,67 27,202 26,101 25,304 24,152 23,76 22,38 21,19 20,58 19,29 18,88 17,44 16,22 15,11 14,34 13,17 12,52 11,26 10,13 9,40 8,20 7,10 6,5 5,16 4,8 3,4 2,2 1,1
Stored as a CSV file you can import it and run this code:
# Collatz plot ------------------------------------------------------------ library(sys) a <- collatzdata # collatzdata is a table of a Collatz sequence # starting with 9870987, which has two columns: # run -> the running number of the sequence # value -> the pertinent number # | run | value | # ------------------ # | 146 | 9870987 | # | 145 | 29612962 | # | ... | ... | i=150 while(i>=1){ b<-filter(a,run<i+1) plot(b,type="l") Sys.sleep(.1) i=i-2 print(paste("Iteration # ",i),quote=FALSE) }
You may watch this YT video to see it performing: