4 months ago
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
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:
let sum : felt = squared_summation(felt_array + 1, felt_array_size - 1)
return (sum + [felt_array] * [felt_array])
ㅤ This answer was originally posted on Triality
4 months ago
Cairo: How do I initialize an array in cairo
Cairo: How do I iterate through an array of structs?
How can I use dynamic allocation in Cairo?
Getting the CLASS_HASH of a contract in Cairo
Cairo: Iterating through arrays
How to calculate the sum of the digits of a felt in Cairo?
How to make math operation with Field Elements (felts) in Cairo lang?
How to verify Empiric’s Data Entries Using Events?
How can I use start_prank with deploy_contract?
Equivalent to Solidity's keccak256(abi.encodePacked())?
A switch statement in Cairo lang to set two distinct values
Is it possible to import local python files into hints?
Cairo Error: Getting Error while setting value of item in array
How can I verify a contract on starkscan or voyager after deploying the contract