a neat picture based on recursive number

A place to discuss the science of computers and programs, from algorithms to computability.

Formal proofs preferred.

Moderators: phlip, Moderators General, Prelates

phillip1882
Posts: 145
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

a neat picture based on recursive number

https://ibb.co/kPRY2z
this picture represents the first 4000 numbers in recursive format.
<> 2
<<>> 3
<><>4
<<<>>> 5
and so on
the left arrow turns the turtle left 30 degrees then moves forward 5 and the right arrow rotate right 60 degrees and then move forward 5
heres the python code that produced it

Code: Select all

`import turtlerecurse = [""]*1000000recurse[2] = "<>"n = 2p = 1while n < len(recurse):   for v in range(2,int(len(recurse)/n)):      if recurse[v] != "":         recurse[v*n] = recurse[v] +recurse[n]        n += 1   while n<len(recurse) and recurse[n] != "":      n += 1   if n == len(recurse):      break   p += 1   recurse[n] = "<" +recurse[p] +">"turtle.screensize(7000,4500)turtle.up()   turtle.sety(-200)turtle.down()for i in range(2,len(recurse)):   for j in range(0,len(recurse[i])):      if recurse[i][j] == ">":         turtle.right(60)         turtle.forward(5)      else:         turtle.left(30)         turtle.forward(5)   print(recurse[i],i)input()`
good luck have fun

Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

Re: a neat picture based on recursive number

First, neat picture! It's always cool to see the kind of emergent patterns that come out of simple rules like this.

Second, the few numbers you provided give *no* clue as to the pattern you're following. I had to puzzle thru your Python to figure it out instead. From what I can tell, the pattern is:

1. The 1st pattern, P₁, is the empty string.
2. If n is the mth prime, then Pₙ = "<" + Pₘ + ">". (So, since 2 is the 1st prime, P₂ = <>, which is P₁ wrapped in angle brackets. 3 is the 2nd prime, so it's <<>>, P₂ wrapped. 5 is P₃ wrapped, 7 is P₄ wrapped, 11 is P₅ wrapped, etc.)
3. If n is composite, it can be divided into its smallest prime factor j and the rest of the number k. Pₙ is then Pₖ+Pⱼ. (So P₄ is P₂+P₂ <><>, P₁₂ is P₄+P₃ <><><<>>, P₁₀₀ is P₅₀+P₂, etc.)

Third, your use of single-letter variables and some slightly unidiomatic Python made it a bit hard to follow. I've rewritten it here to help myself understand:

Code: Select all

`#!/usr/bin/env python2def generatePatterns(limit):   patterns = [""] * limit   prime = 2   primeCount = 1   while prime < limit:      # If N is the Mth prime, generate its pattern from the Mth pattern.      patterns[prime] = "<" + patterns[primeCount] + ">"      # Generate patterns for every possible multiple of prime      # from the Prime'th pattern      # and the Multiplier'th pattern      for mult in xrange(2): # we won't use the whole range         if mult*prime >= limit:            break         if patterns[multiplier]:            patterns[multiplier*newPrime] = patterns[multiplier] + patterns[newPrime]      # Now find the next prime.      # Per Eratosthene's sieve, this is just the next unfilled value.      while prime < limit and patterns[prime]:         prime += 1      primeCount += 1   return patternsdef drawPatterns(patterns):   import turtle   turtle.screensize(7000,4500)   turtle.up()   turtle.sety(-200)   turtle.down()   for pattern in patterns:      for command in pattern:         if command == ">":            turtle.right(60)            turtle.forward(5)         else:            turtle.left(30)            turtle.forward(5)      print(pattern,i)drawPatterns(generatePatterns(10**6))`

I haven't run this, but I think it works? Is this code clear to you?
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

phillip1882
Posts: 145
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

Re: a neat picture based on recursive number

some small mistakes you made, i corrected them. mostly just using variables without declaring them

Code: Select all

`def generatePatterns(limit):   patterns = [""] * limit   prime = 2   primeCount = 1   while prime < limit:      # If N is the Mth prime, generate its pattern from the Mth pattern.      patterns[prime] = "<" + patterns[primeCount] + ">"      # Generate patterns for every possible multiple of prime      # from the Prime'th pattern      # and the Multiplier'th pattern      for mult in range(2,len(patterns)): # we won't use the whole range         if mult*prime >= limit:            break         if patterns[mult]:            patterns[mult*prime] = patterns[mult] + patterns[prime]      # Now find the next prime.      # Per Eratosthene's sieve, this is just the next unfilled value.      while prime < limit and patterns[prime]:         prime += 1      primeCount += 1   return patternsdef drawPatterns(patterns):   import turtle   turtle.screensize(7000,4500)   turtle.up()   turtle.sety(-200)   turtle.down()   i = 0   for pattern in patterns:      for command in pattern:         if command == ">":            turtle.right(60)            turtle.forward(5)         else:            turtle.left(30)            turtle.forward(5)      print(pattern,i)      i+=1drawPatterns(generatePatterns(10**6))`
good luck have fun

FlatAssembler
Posts: 66
Joined: Fri Oct 27, 2017 7:42 pm UTC

Re: a neat picture based on recursive number

Didn't know Python supported turtle graphics, thanks for informing me.
Anyway, I've done a similar thing in JavaScript about a year ago. You can see it here. Though, admittedly, your looks even better.

PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: a neat picture based on recursive number

Also see Dyck words.
In the theory of formal languages of computer science, mathematics, and linguistics, a Dyck word is a balanced string of square brackets [ and ]. The set of Dyck words forms Dyck language.

Dyck words and language are named after the mathematician Walther von Dyck. They have applications in the parsing of expressions that must have a correctly nested sequence of brackets, such as arithmetic or algebraic expressions.

phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: a neat picture based on recursive number

Code: Select all

`enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}`
[he/him/his]

Xanthir
My HERO!!!
Posts: 5410
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

Re: a neat picture based on recursive number

Ah, indeed! I hadn't quite fully gathered the recursive nature there. So you're expressing a number solely by using the nth-prime() function, multiplication, and the number 1. For compactness, nth-prime(arg) is instead written as <arg>, multiplication is implicit from concatenation, and 1 can be omitted because it only and always appears at the deepest nesting of a <>. So 7 is nth-prime(4), aka nth-prime(2*2), aka nth-prime(nth-prime(1) * nth-prime(1)), and this can then be compacted to <<><>>.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

phillip1882
Posts: 145
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

Re: a neat picture based on recursive number

https://ibb.co/dxoNzp
another one for fun. its red for an even number of prime factors and blue for odd. its also toroidal, it wraps around the edges. also i made the right 45 rather than 60, and forward 3 rather than 5
this picture is roughly 1/4 the whole image.

Code: Select all

`import timeimport turtlerecurse = [""]*1000000recurse[2] = "<>"n = 2p = 1while n < len(recurse):   for v in range(2,int(len(recurse)/n)):      if recurse[v] != "":         recurse[v*n] = recurse[v] +recurse[n]        n += 1   while n<len(recurse) and recurse[n] != "":      n += 1   if n == len(recurse):      break   p += 1   recurse[n] = "<" +recurse[p] +">"turtle.screensize(2000,2000)turtle.up()   turtle.sety(-200)turtle.down()turtle.speed(0)turtle.hideturtle()for i in range(2,len(recurse)):   for j in range(0,len(recurse[i])):      x,y = turtle.position()      if x < -1000:         turtle.up()         turtle.setx(1000)         turtle.down()      elif x > 1000:         turtle.up()         turtle.setx(-1000)         turtle.down()      if y <-1000:         turtle.up()         turtle.sety(1000)         turtle.down()      elif y > 1000:         turtle.up()         turtle.sety(-1000)          turtle.down()      middle = 0      total = 0      for k in range(0,len(recurse[i])):         if recurse[i][k] == "<":            middle += 1         else:            middle -= 1         if middle == 0:            total +=1      if total&1 == 0:         turtle.color("red")      else:         turtle.color("blue")      if recurse[i][j] == ">":         turtle.right(45)         turtle.forward(3)      else:         turtle.left(30)         turtle.forward(3)          print(recurse[i],i)time.sleep(86400)input()`
good luck have fun

phillip1882
Posts: 145
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

Re: a neat picture based on recursive number

https://ibb.co/bYDXKz

this one is green for squares or multiple of squares. this picture is after roughly 2000. i don't think posting the code is necessary, its fairly straight forward
good luck have fun

phillip1882
Posts: 145
Joined: Fri Jun 14, 2013 9:11 pm UTC
Location: geogia
Contact:

Re: a neat picture based on recursive number

final one: the result after 200,000 approximately, it mostly just looks like a random plot
https://ibb.co/ff53Gp
good luck have fun

Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 4060
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: a neat picture based on recursive number

"Looks like a random plot" is at one end of a spectrum of expectations. "Looks exactly like it's what you put in" is at the other end.

Unless you're looking for that kind "procedural fuzziness" output to produce (reproducable, arbitrarily detailed 'naturalistic' distribtions at will, the more interesting ones are in the middle. They don't look "random" but they have no obvious relationship with the specification chosen to produce them. Like seeding a Perlin Noise and somehow getting the impression of scrawled 'letter' shapes saying "This was just the first death!! I shall kill again!!!!".as a result.

That said, I like what you're looking at. I assume you already knew about the likes of the Dragon Curve, BTW?

FlatAssembler
Posts: 66
Joined: Fri Oct 27, 2017 7:42 pm UTC

Re: a neat picture based on recursive number

Here is a picture I made in my own programming language a few days ago:

Code: Select all

`;Mathematical example: The Polar Rose.AsmStart   format PE console   entry start   include 'win32a.inc'   section '.text' code executable   start:   invoke system,_cls   invoke system,_setGreenForegroundAsmEndy:=0While y<24   x:=0   While x<80      distance:=sqrt(pow(abs((x-40)/2),2)+pow(abs(y-12),2))      angle:=atan2(y-12,(x-40)/2)      r:=cos(angle*3)*12      If (distance < (r + 0.5) ) | ( (y = 12) & (x > 40-1) & (x < 40 + (13 * 2) ) )         AsmStart            jmp starSign\$            starSign:               db '*',0            starSign\$:            sub esp,4            mov dword [esp],starSign            call [printf]         AsmEnd      Else         AsmStart            jmp spaceSign\$            spaceSign:               db ' ',0            spaceSign\$:            mov dword [esp],spaceSign            call [printf]         AsmEnd      EndIf      x:=x+1   EndWhile   AsmStart      jmp newLineSign\$      newLineSign:         db 10,0      newLineSign\$:      sub esp,4      mov dword [esp],newLineSign      call [printf]   AsmEnd   y:=y+1EndWhileAsmStartinvoke system,_pauseinvoke system,_restoreColorsinvoke system,_clsinvoke exit,0_cls db "CLS",0_setGreenForeground db "COLOR 0A",0_pause db "PAUSE",0_restoreColors db "COLOR 07",0section '.rdata' readable writableresult dd ?x dd ?y dd ?distance dd ?angle dd ?r dd ?section '.idata' data readable importlibrary msvcrt,'msvcrt.dll'import msvcrt,printf,'printf',system,'system',exit,'exit',scanf,'scanf'AsmEnd`

Who is online

Users browsing this forum: No registered users and 3 guests