/ Published in: PHP
Expand |
Embed | Plain Text
<?php $c = new Collatz(); class Collatz { private $steps; public function __construct() { $this->steps = 0; } public function steps() { return $this->steps; } public function run($n) { $result; if ($n == 1) { $result = 1; } else if ($n % 2 == 0) { $result = $this->run($n / 2); } else { $result = $this->run($n * 3 + 1); } if ($n != 1) $this->steps++; return $result; } }
You need to login to post a comment.
