Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Fast exponentiation

Core exercise

Write puissance(long base, int exposant) that computes base^exposant by fast exponentiation: instead of multiplying exposant times, you square and divide the exponent by 2.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

public class Exercices {

    public static void main(String[] args) {
        System.out.println("2^10 = " + puissance(2, 10));
    }

    public static long puissance(long base, int exposant) {
        // TODO : exponentiation rapide
        return 0;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

If the exponent is even: x^n = (x*x)^(n/2). If it is odd: set one factor x aside. About 10 iterations are enough for an exponent of 1000.