How to do pow/exponentiation in cairo1?

How to do pow/exponentiation in cairo1?

@rickju Here’s an example on how to do to pow in cairo

fn fpow(x: u256, n: u8) -> u256 {
    let y = x;
    if n == 0 {
        return 1;
    }
    if n == 1 {
        return x;
    }
    let double = fpow(y * x, n / 2);
    if (n % 2) == 1 {
        return x * double;
    }
    return double;
}

Or you can import the Alexandria library onto your codebase and use it Here’s the link

3 Likes