Coding, Computer Science, Crafty, Logic, Python, Simon's Own Code

A Small Program that Doubles Itself

I wrote a small program that copies itself. When the program doubles itself it executes itself twice. The code that doubles itself is now doubled. The second time you run it you will get 8 times its original copy. The following time it’s going to double 8 copies of itself 8 times. Afterwards it doubles 2048 copies of itself 2048 times — that I can’t run because it would overwhelm the universe 5 times!

Coding, Geometry Joys, Murderous Maths, Physics, Python, Simon teaching, Simon's Own Code, Simon's sketch book

Physics Engine using Verlet Integration

Simon created a physics engine in Python with Turtle. He used Verlet integration (French pronunciation: ​[vɛʁˈlɛ]), a numerical method for integrating Newton’s equations of motion in calculating trajectories of particles in molecular dynamics simulations and computer graphics.

see Simon’s interactive sketch in Geogebra at https://www.geogebra.org/m/neuxj63g

Verlet Integration is a way to implement a physics engine without having to care about velocity.

Instead of storing the velocity, you store the previous position, and you calculate the velocity on the fly. Then if you add that velocity to the current position, you get the new position. But then you also have to add on the acceleration, because acceleration changes velocity.

Coding, Community Projects, Contributing, JavaScript, Math and Computer Science Everywhere, Milestones, Murderous Maths, Notes on everyday life, Simon makes gamez, Simon's Own Code, Together with sis

Spring Challenge 2020 PacMan in p5.js

Simon has recreated the CodingGame.com’s Spring Challenge 2020 PacMan game in p5.js to be able to work on the AI versions after the spring challenge has finished.

Link to Simon’s PacMan Game version featured in the video (playable for two players on the same keyboard): https://editor.p5js.org/simontiger/present/k9PDqMeew

Simon’s code for this version: https://editor.p5js.org/simontiger/sketches/k9PDqMeew

The PacMan is built on top of a Maze Generator, here’s an example of one of Simon’s maze generators and solvers: https://editor.p5js.org/simontiger/sketches/vj75cHYkf

Simon had been taking part in the Spring Challenge 2020 for several days and reached bronze level.

However he quickly realized that the 11 days of the competition felt too cramped for him to try various algorithms and still be able to work on his other projects. So what he did was recreate the whole PacMan game from scratch in p5.js, so that he has an “archived version” of the challenge and can play with new AI versions later.

art, Coding, Crafty, Milestones, Notes on everyday life, Python, Simon teaching, Simon's Own Code, Together with sis

Making small animations with Python turtle

This is what I got from the kids yesterday as my Mother’s Day present. Simon has taught Neva to make little animations in Python.

This is another little video of them using working together to create a poppy flower drawing with Python turtle:

Coding, Community Projects, Contributing, Geometry Joys, Group, live stream, Milestones, Murderous Maths, Notes on everyday life, Physics, Python, Simon's Own Code

Live Session with Stephen Wolfram and the Wolfram Demo Project, World Science Scholars.

Simon has completed the course A New Kind of Science with Stephen Wolfram and the World Science Scholars program. Which doesn’t mean he is done with digging deep into Wolfram’s groundbreaking new kind of science! (As a matter of fact, he is still reading Wolfram’s 1500-page book. And as Professor Wolfram told Simon during the live session, there’s nothing in the book that no longer holds).

Simon happy after a major break-through in his demo project, hoping to present his findings to Stephen Wolfram the same evening

At the live session, a few scholars including Simon were planning to present their Wolfram Language demos, but Professor Wolfram was so inspired by his current research that he decided to share his latest discoveries instead (he is tiptoeing closer to laying the foundations of a theory that would unify all natural sciences based on his principle of computational equivalence). It was a very engaging session (even though Simon’s video camera malfunctioned, which hardly mattered).

screenshot of the live session
Danielle Rommel, who works with Professor Wolfram, told Simon she had actually been watching videos on his YouTube channel!

As for Simon’s demo project, that’s a whole story. It took Simon weeks to define what he was actually going to pick as his topic and once he had picked his topic, he didn’t know where to start (because he managed to pick an NP problem). He suggested to collaborate together with another World Science Scholar, as it was that boy who initially inspired to think in the direction of the particular open math problem. The two of them had two long video chats. (It was so much fun to observe them, they both had zero interest in small talk and went straight down to the math, without even saying hi).

Simon during a video chat with a fellow student, discussing the project
ways to write graph data that Simon shared with his fellow student during their talk

Unfortunately, after the original project presentation during the live session with Stephen Wolfram was cancelled, Simon’s partner never really replied to Simon’s chat messages (until weeks later). Simon did manage to get part of the demo done (porting a huge graph into the Wolfram Language, which required writing separate code in Python), but felt stuck later, after several attempts to color the graph failed. He ended up spending several days writing several more Python scripts. We have documented the process on video. The project has turned into a computational essay and it’s definitely still unfinished, but I’m not sure Simon will come back to it in the near future. He got a couple of minutes to present his findings at another live session last week (with a World Science Scholars teaching fellow Aaron Mertz and Rory Foulger, Education Outreach Coordinator at Wolfram Research), but was confused as he didn’t get any feedback about his findings and got the impression his main questions weren’t understood. He was also a bit annoyed with me yelling on the background about what he should do and say (I saw he was confused and was afraid his time would be cut short, so I wanted to make sure he would mention his main points). I’ve learned my lesson now and have decided not to interfere with his live performances anymore, not to put him under additional pressure.

Simon has also written to Professor Wolfram, currently awaiting his reply. His main questions were:

I was surprised to discover that no Heule or de Grey graphs exist (anymore?) built into the Wolfram Language. As part of my research, I’ve created a very long list of all the graphs the Wolfram Language knows about, and HeuleGraph is not in the list. I tried to pose this question during the short discussion of my project at the World Science Scholars live session this week, but didn’t get any feedback (I don’t think my question was understood). Yes, one is able to find images of Heule graphs in Wolfram notebooks, (like this one https://notebookarchive.org/heule-graph–2019-07-0z3zu9k/) and on Wolfram MathWorld (like here http://mathworld.wolfram.com/HeuleGraphs.html). But those are just pictures in archived notebooks, and even if I try to copy/paste the code into my own notebook, it doesn’t work.

My second question concerns coloring such a large graph in the Wolfram Language: do you think it could be possible? As I don’t know a built-in function to do that within the Wolfram Language (and I don’t think such a thing exists), I decided to try to color the graph in Python and then upload it into my Wolfram notebook. I created another Python script to make the graph easier to color, and yet another Python script to actually color all the vertices (using Breadth-First Search). The problem was: it didn’t color it with only 5 colors (but with 8)! I made a video about the making of the project, with me explaining why this task is hard for a computer to do, and even some computational complexity theory!

Timecodes: Converting to CSV: 0:00 Generating the Colors: 23:06 Some Math: 42:16 Part I Conclusion: 56:46

this video is long, but even briefly scanning through its several parts gives a thorough impression of Simon’s current math and coding abilities

The project is attempting to visualize the Hadwiger–Nelson problem from geometric graph theory: what is the minimum number of colors required to color the plane (chromatic number of the plane) such that no two points at distance 1 from each other have the same color. It’s an unsolved problem, but we know that the answer is 5, 6 or 7. In 2018, Aubrey de Grey proved that the chromatic number of the plane is at least 5. His smallest unit-distance graph with chromatic number 5 had 1581 vertices. Several smaller graphs have been found since then, a major contribution done by Marijn Heule, who has come up with his own method of reducing the size of graphs. In 2019, Heule constructed the smallest unit-distance graph with chromatic number 5 so far, with 517 vertices. (Side-note: since I decided I’m going to use the 517 graph, I have actually found a smaller Heule graph with 508 vertices, but it was of a data format that I wasn’t able to use anyway). The goal of my project was to color such a graph in Wolfram language, to create a Wolfram Demo.

In Part 2, I tried to code yet another Python script to group the graph into smaller units to make a smaller graph, and color that one, then blow each vertex back into the unit considered.

Link to Simon’s Wolfram Notebook: https://www.wolframcloud.com/obj/9795e37e-aa73-4ae6-8249-81223ffdbc7f Link to my code on GitHub: https://github.com/simon-tiger/Hadwiger-Nelson-Project-Data

Simon reading Marijn Heule’s paper “Computing Small Unit-Distance Graphs with Chromatic Number 5”

Link to Marijn Heule’s paper “Computing Small Unit-Distance Graphs with Chromatic Number 5”: https://arxiv.org/pdf/1805.12181.pdf

Coding, Experiments, Math Tricks, Milestones, Murderous Maths, Notes on everyday life, Python, Simon's Own Code, Simon's sketch book

Simon’s Formula to Check Triangle Numbers

Simon spent the morning of December 5 pondering about how to test whether a number is a triangle number. “To test if something is a triangle number: double it, ask if it’s a multiple of its own square root. If that square root has a decimal, round it down”. This was his initial hypothesis, later discarded.

Simon jotting equations on a piece of cardboard while I drag him to his French lesson as we’re running desperately late. I feel bad about interrupting his train of thought.

Another formula he came up with was if n is even, m is a triangle number. After we got back home, he quickly wrote some code to check it:

Coding, Murderous Maths, Python, Simon teaching, Simon's Own Code, Simon's sketch book

Prime Generation Algorithm in Python

Simon has written a code in Python that generates primes using the finite list from Euclid’s proof that there are infinitely many primes. “Starting with one prime (2) the code uses the finite list to generate a couple more numbers that aren’t in the list but are primes. It may not even get to all the primes in the long run!” There is only one problem with Simon’s algorithm…

Simon has written down Euclid’s proof in his own words first https://imgur.com/ML2tI6n
and then decided to program it in Python.

Resources:
https://www.programiz.com/python-programming/methods/list/remove
https://www.geeksforgeeks.org/iterate-over-a-set-in-python/
https://www.youtube.com/watch?v=OWJCfOvochA
https://numbermatics.com/n/10650056950807/
https://defuse.ca/big-number-calculator.htm

Coding, English and Text-Based Data, Python, Simon teaching, Simon's Own Code

It takes the sun to the ground, and violet on the observer’s eye.

Simon writes:

This amazing sentence is generated by a Markov Text-Generation Algorithm. What is a Markov Algorithm? Simply put, it generates the rules from a source text, and it generates a new text that also follows those rules. The rules are often called the Markov Blanket, and the new text is also called the Markov Chain. OK, how does this all work?

Let’s take an example: let’s consider the source text to be “Hello, world!”. Then we pick a number called the order. The higher the number, the more sense the text makes. We’ll pick 1 for the first examples, we’ll examine what happens with higher numbers later.

Then we generate the Markov Blanket. This is a deterministic process. We start from the beginning: “H”. So we put H in our Markov Blanket. Then we come across “e”. So we put e in our Markov Blanket, but to specify that it’s next from H, we connect H to e by an arrow. Then we stumble on “l”. So we put l in our Markov Blanket, but again, to specify that it’s next from e, we connect e to l by an arrow.

Now, here’s where it gets interesting. What’s next? Well, it’s “l” again. So now we connect l to itself, by an arrow. This is interesting because it’s no longer a straight line!

And we keep going. Once we’re done, our Markov Blanket will look something like this:

Once we’ve created our Markov Blanket, then we start generating the Markov Chain from it. Unlike the Markov Blanket, generating the Markov Chain is a stochastic process.

This is just a process of wandering about the Markov Blanket, and noting down where we go. One way to do this, is just to start from the beginning, and follow the path. And whenever we come across some sort of fork, we just spin a wheel to see where we go next. For example, here are some possible Markov Chains:

Held!
Helld!
Hellld!
Helorld!
Hello, world!
Helllo, wo, wo, world!

That was an easy one, so let’s do something more complex with code.

First, just an interface to enter in the text, and the order:

text = "" # Variable to hold the text

print("Type your text here (type END to end it):")

while True:
  line = input("") # Read a line of text from standard input
  if line != "END": text += line + "\n" # If we didn't enter END, add that line to the text
  else: break # If we entered END, signify that the text has ended

text = text[:len(text)-1] # Remove the last line break

order = int(input('Type the order (how much it makes sense) here: '))

input("Generate me a beautiful text") # Just to make it dramatic, print this message, and ask the user to hit ENTER to proceed

Next, the Markov Blanket. Here, we store it in a dictionary, and store every possible next letter in a list:

def markov_blanket(text, order):
  result = {} # The Markov Blanket

  for i in range(len(text) - order + 1): # For every n-gram
    ngram = ""
    for off in range(order):
      ngram += text[i+off]
    
    if not ngram in result: # If we didn't see it yet
      result[ngram] = []
    if i < len(text) - order: # If we didn't reach the end
      result[ngram].append(text[i+order]) # Add the next letter as a possibility
  
  return result # Give the result back

Huh? What is this code?

This is what happens when we pick a number >1. Then, instead of making the Markov Blanket for every character, you make it for every couple of characters.

For example, if we pick 2, then we make the Markov Blanket for the 1st and 2nd letter, the 2nd and 3rd, the 3rd and 4th, the 4th and 5th, and so on. When we generate the Markov Chain, we squash the ngrams that we visit together. So next, the Markov Chain:

def markov_chain(blanket):
  keys = blanket.keys()
  ngram = random.choice(list(keys)) # Starting Point
  new_text = ngram
  while True:
    try:
      nxt = random.choice(blanket[ngram]) # Choose a next letter
      new_text += nxt # Add it to the text
      ngram += nxt # Add it to the ngram and remove the 1st character
      ngram = ngram[1:]
    except IndexError: # If we can't choose a next letter, maybe because there is none
      break
  return new_text # Give the result back

# Now that we know how to do the whole thing, do the whole thing!
new_text = markov_chain(markov_blanket(text, order), num)
print(new_text) # Print the new text out

OK, now let’s run this:

Type your text here (type END to end it):
A rainbow is a meteorological phenomenon that is caused by reflection, refraction and dispersion of light in water droplets resulting in a spectrum of light appearing in the sky. It takes the form of a multicoloured circular arc. Rainbows caused by sunlight always appear in the section of sky directly opposite the sun.
Rainbows can be full circles. However, the observer normally sees only an arc formed by illuminated droplets above the ground, and centered on a line from the sun to the observer's eye.
In a primary rainbow, the arc shows red on the outer part and violet on the inner side. This rainbow is caused by light being refracted when entering a droplet of water, then reflected inside on the back of the droplet and refracted again when leaving it.
In a double rainbow, a second arc is seen outside the primary arc, and has the order of its colours reversed, with red on the inner side of the arc. This is caused by the light being reflectedtwice on the inside of the droplet before leaving it.
END
Type the order (how much it makes sense) here: 5
Generate me a beautiful text

And……..it..stops.

Why did it do that?

You see, this is not such a good method. What if our program generated a Markov Blanket that didn’t have an end? Well, our program wouldn’t even get to the end, and it would just wander around and around and around, and never give us a result! Or even if it did, it would be infinite!

So how do we avoid this?

Well, we set another much bigger number , let’s say 5000, to be a callout value. If we don’t get to the end within 5000 steps, we give up and output early. Let’s run this again…

And now, it doesn’t stop anymore! Snippets of example generated text:

It takes the sun to the ground, and violet on the observer’s eye.

This rainbow, a second arc formed by illuminated droplets resulting it.
In a primary rainbow is a meteorological phenomenon the back of the ground, and has the sky. It takes the order of its coloured circles. However, the sun.

Rainbow, a second arc shows red on a line from the section of light in water droplet and has the sun.

In a double rainbow is caused by illuminated droplet on the outer part and refracted when leaving in a spectrum of a multicoloured circles. However, the droplet of water droplets resulting it.
In a double rainbow is a meteorological phenomenon the droplets resulting in a spectrum of a multicoloured circular arc. Rainbow is caused by the inner side the observer’s eye

Play with this project online at: https://repl.it/@simontiger/Markov-Text