Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function collatz($originalNumber){ //the collatz conjecture function
- $number = $originalNumber;
- do{
- $i++;
- if(1&$number){
- $number = (3*$number)+1;
- }else{
- $number = $number/2;
- }
- }while($number != 1);
- return $i;
- }
- echo "Collatz Conjecture\n"; // :D
- $n = 1;
- do{ //could've used a for loop, but I think this worked out better.
- $i = collatz($n); //testing how many steps that it will take for the number $n to reach 1.
- if($i > $maxheight){ //if the result is larger than all the rest, the new result will become the largest result.
- $maxheight = $i;
- }
- $x[$n] = $i; //$n is the position along the x-axis, $i is the position along the y-axis.
- $n++; //next number
- }while($n < 1000); //find the amount of steps that it takes for numbers 1 to one thousand... which is probably a little high. CHANGE THIS DEPENDING ON HOW MANY NUMBERS YOU WANT TO TEST
- //---begin writing file
- unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
- $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
- $width = $n; //width of the image is the largest number that was tested
- $height = $maxheight; //height is the largest amount of steps that it took to reach zero
- $startWrite = "P3\n# image.ppm\n".$width." ".$height."\n255\n"; //fomatting the ppm file
- fwrite($fh, $startWrite);
- $Nl = " ";
- for($k = 0; $k < $width*$height; $k++){
- $CurPx = $k%$n; //basic modulus to decide when a new line must be made
- if($CurPx == 0){ //^ditto^
- $CurPy++;
- }
- if($x[$CurPx] == $height-$CurPy){ //basic algebra
- $out = "255"; //if the if statement above reflects the array which was made on line 23, the pixel will be white
- }else{
- $out = "000"; //else, it will be black
- }
- $write = "$out $out $out$Nl"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- if($CurPx%5 == 0){ //basic formatting decides when a new line must be placed
- $Nl = "\n";
- }else{
- $Nl = " ";
- }
- }
- fclose($fh); //close the file, done!
- ?>
Advertisement
Add Comment
Please, Sign In to add comment