manyspikes

While loops

Initialising environment...

Instructions

Define the following variables:

n = 42 iterations = 0

While the value of n is different from 1, apply the following logic:

  • If n is even, divide it by 2
  • If n is odd, triple it and add one
  • At every iteration, increment the value of the variable iterations by 1

Note: How do we know this loop always finishes, i.e. that the value of n will eventually reach 1? It is called the Collatz conjecture and it is an unsolved mathematical problem which asks whether repeating the rules above transforms every positive integer into 1. To date, no one has been able to show that a positive integer exists that violates the conjecture.