echo utopianTree(5)."\n";
//Using for loops
function utopianTree($n) {
	$val = 1;

	for ($i = 0; $i < $n; $i++) {
		if ($i%2 == 0) {
			//doubled for each odd year
			$val *= 2;
		} else {
			//increased by one for each even year
			$val++;
		}
	}
	return $val;
}
by ვაჟა ტყემალაძე
2 years ago
PHP
Problem Solving
php
Note
Problem
0
Pro tip: use ```triple backticks around text``` to write in code fences
0
ვაჟა ტყემალაძე 2 years ago
echo utopianTree(5)."\n"; function utopianTree($n){ $val=1; if ($n%2){ $val=2*(pow (2,(floor($n/2) + 1)) - 1); }else{ $val=pow(2, (floor($n/2 + 1)) - 1); } return $val; }
REPLY