Kata of the Day #16

Powers of 2

kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
Kata of the Day #16

Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).

TypeScript


function powersOfTwo(n: number): number[] {
    let acc: number[] = [];

    for (let i = 0; i <= n; i++) {
        acc.push(Math.pow(2, i));
    }

    return acc;
}

console.log(powersOfTwo(10));

Go


package main

import "fmt"

func powersOfTwo(n int) []int {
    acc := make([]int, n+1)

    for i := 0; i <= n; i++ {
      acc[i] = int(math.Pow(2, float64(i)))
    }

    return acc
}

func main() {
	fmt.Println(powersOfTwo(10))
}

Python


def powers_of_two(n):
    acc = []
    for i in range(n+1):
        acc.append(2**i)
    return acc

print(powers_of_two(10))

PHP


function powersOfTwo(int $n): array {
    $acc = [];

    for ($i = 0; $i <= $n; $i++) {
        $acc[] = pow(2, $i);
    }

    return $acc;
}

print_r(powersOfTwo(10));

Go to Kata
ByJulie Kohl

Need some help?

Want us to give you some help with your business? No problem, here's a video on who we can help, and how, along with our calendar so you can book in a call to discuss us working together.

Let's see
budget
budget
budget
budget
budget
budget
budget
budget
budget
budget

Subscribe to our newsletter

subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe