-6

A function with the following signature: function exponentArr($num) { }

Such that when $num = 10, the function returns an array of

[1,2,4,8,16,32,64,128,256,512,1024]

Asik
  • 7,967
  • 4
  • 28
  • 34

3 Answers3

5

You can do this:

function exponentArr($num){
    $arr = array();
    for($i=0;$i <= $num;$i++){
        $arr[$i] = pow(2, $i);
    }
    return $arr;
}

This will give you an array $arr with the required output.

Moid
  • 1,447
  • 1
  • 13
  • 24
-2

Try this, Working as you mentioned.

function powArr($number){
    for($i=0;$i < $number;$i++){
        $array[$i] = pow(2, $i);
    }
    return $array;
}
  • This is (almost) the same as Moid's answer, except his is correct, more complete and has an explanation. -1. – Styphon Jan 28 '15 at 11:50
-3

This is Fibonacci sequence

Try this:

$fib = [1,0];
for($i=0; $i<$num; $i++) {
    $next = array_sum($fib);
    array_shift($fib);
    array_push($fib,$next);
    echo $next.", ";
}
Ruben Lech
  • 125
  • 12