NEWTON

NEWTON


Popular tags

    Cairo: Getting the squared summation of felts in an array

    Asked

    4 months ago

    4

    views


    0

    Given an array of elements [2,3,4] I want to be able to get the squared summation of the individual elements, 2^2 + 3^2 + 4^2.

    How can I achieve this?

    This question was originally posted on Triality

      cairocairo-lang

    1 answers

    0

    Use the following recursive function to get the result you are looking for

    func squared_summation(felt_array : felt*, felt_array_size : felt) -> (res : felt):

    if felt_array_size == 0:

    return (0)

    end

    let sum : felt = squared_summation(felt_array + 1, felt_array_size - 1)

    return (sum + [felt_array] * [felt_array])

    end

    This answer was originally posted on Triality

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON