Collatz conjecture

Learning project: PHP

02/2023

The Collatz conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.

What’s the program about?
Although the case remains unsolved, it became a valuable exercise to compile an algorithm that tests the Collatz conjecture. The presented PHP program assumes the user enters a number (positive or negative) to start the series. The results show indeed that starting with any positive number, the sequence ends at 1, and starting with a negative number, the sequence ends at either -1, -5 or -17.

PHP
<?php echo '<h1>Collatz conjecture calculator</h1> <form action="collatz_sequence_calculator.php" method="POST"> <lable>Choose a number to start from: </lable> <input type="number" name="choosen_num" id="choosen_num"> <input type="submit" name="start_calculation" value="Start"> <p>or</p> <lable>Calculate with a random number: </lable> <input type="submit" name="generate_num" value="Generate"> <input type="hidden" name="generated_num" value= "' . $generated_num = rand(-100, 100) . '"> </form>'; function collatzCalc($num) { $num_array = array(); while (in_array($num, $num_array) == false) { array_push($num_array, $num); if ($num % 2 == 0) { $num = $num/2; } else { $num = ($num*3)+1; } } array_push($num_array, $num); $remove_the_last_element = array_pop($num_array); foreach($num_array as $array_element) { echo $array_element . ", "; } } echo '</br></br>'; if(isset($_POST['start_calculation']) && $_POST['choosen_num']!="") { $num = $_POST['choosen_num']; echo 'The choosen number is ' . $num; echo '</br></br>'; collatzCalc($num); } else { if(isset($_POST['generate_num']) && $_POST['generated_num']!="") { $num = $_POST['generated_num']; echo 'The generated number is ' . $num; echo '</br></br>'; collatzCalc($num); } } ?>
Library – Simple XSLT processor
Draw triangle from stars – PHP