# Python Algorithm

## Introduction

**LEVEL: BEGINNER**

Expected Time: 30 min

In this tutorial you will learn how to create a simple Python algorithm. The tutorial will consist out of three parts. First you will learn how to make a Python script that calculates the result of a complex formula. Next, you will learn how to implement a conditional algorithm, using the Collatz Conjecture as example. Finally, we will implement this script in a looped version.

## Complex formula

Add a Python node to the Grasshopper canvas. Connect one Number Slider to the x input and one Number Slider to the Y input.

- Add a Python node to the canvas
- Connect a Number Slider to x and a Number Slider to y

For good practice, change the input names and the output names. This can be done by right-clicking on the in- or output and setting a new name in the top bar.

- Rename the inputs and outputs

Change the input types of x and y to floats. Floats are finite decimal numbers.

- Set the input types to floats

Open the Grasshopper Python Script Editor.

- Open the Grasshopper Python Script Editor

- Type a useful description of the inputs and outputs in line 1 to 6

Although most mathematical operators are available for Python without importing any libraries, it may be wise to import the Math library. This will give us access to more complex functions like cosines and square roots. Remove the "import Rhinoscriptsyntax" line. We will not need this module.

- Remove the rhinoscriptsyntax import statement
- Import the math library

Now we need to code the algorithm. Type the following code according to the right illustration.

- Type your algorithm
- Click on OK

By connecting a panel to the output of the node, you will see that Python calculated the result.

- Connect a panel to the output

By using the Math library, you can use complex functions.

Your first algorithm is now finished.

## Collatz Conjecture

The Collatz Conjecture is a simple, but still unsolved problem by mathematicians. Take an arbitrary number. If the number is odd, multiply it by three and add one. If the number is even, divide it by two. Now continue with the next found value. You may notice, that after continuing this for a few steps, the result always seem to end with one. Although this may seem easy to prove, mathematicians have not been able to prove that this is true for all numbers till infinity. The Collatz Conjecture is also known as the "3x + 1 problem".

In the following script, we will replicate this problem, and find out if we can proof that the Collatz Conjecture always comes back to one.

Add a Python node to the canvas. Connect one number slider to the inputs and connect a panel to the output. Rename the inputs and outputs.

- Add a Python node to the canvas
- Connect a Number Slider to one input
- Rename the input and output

Since our input should always be a whole numbers, set the data type to integer.

- Set the input data type to integer

Open the Grasshopper Python Script Editor. Change the description of the input and output. Remove the "import Rhinoscriptsyntax" statement.

- Open the Grasshopper Python Script Editor
- Type a description for the input and output
- Remove the "import rhinoscriptsyntax" line

First we need to check if our input is even or odd. If the number modulus 2 is equal to 0, then it is even. If not, then it is odd. Use an if-else statement to check if the input is even or odd.

- Check if the input is even or odd

Now calculate the result using the 3x + 1 statement and the division by 2 statement.

- Calculate the result

## Collatz Conjecture in while-loop

**LEVEL: INTERMEDIATE**

Expected Time: 15 min

*Before you continue: while-loops are always dangerous to use in Python for Grasshopper. Grasshopper does not have a limit to the amount of iterations that it will calculate. Therefore, if you accidentally make a while-loop that continues forever, Grasshopper will definitely break. Always save your file first, before you implement any while-loops, or add a "break" statement at the end of the loop.*

Finally, using a while-loop, we can let the computer calculate all successive numbers and adding them to the result. First you will need to create an empty list for the result. Next, we put the previously created conditional statement in a while-loop. While the temporary result is not equal to one, it should be added to the final result. If it is equal to one, break the while-loop and add the value one to the result.

- Copy the following script to calculate all successive numbers:

We can easily check the amount of iterations by connecting a List Length node to the output.

- Connect a List Length to calculate the amount of iterations

The result can be used to plot a graph which represents all numbers that were used before the script reaches value one.

To prove the Collatz Conjecture, scientists used brute force on a computer, to calculate as much values as possible. In total, 2^68 values were checked and all ended up to one. ^{[1]} In Python, we can easily replicate a script that solves as much values as possible. Using the following code, Grasshopper was able to find the amount of necessary iterations for 10.000 values in about 1 second.

- ↑ Convergence verification of the Collatz problem, David Barina