NEWTON
Asked
6 months ago
108
views
1
Hello! This is day 4 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Variables”. Can you launch this code with the instructions and write down how many steps the program has in the output?
[BONUS]: try to make it faster
// The following code outputs the 9th power of 2.
// 1. Click on "Debug" to run the code, and then click on
// the arrows in the memory panel to trace the program
// step by step.
//
// Observe that the computation of `z` take 8 steps and
// creates 8 temporary variables. This is because `let`
// simply replaces each instance of `y` with `x * x * x`
// and each instance of `z` with `y * y * y`, which is
// expanded to nine multiplications.
//
// 2. Change the two `let`s to `tempvar`s and run the program.
//
// "tempvar" stands for "temporary variable", which means that
// a variable will be allocated for the intermediate values
// (`x * x * x` and `y * y * y`). This reduces the same
// computation to use only 4 steps and 2 new variables.
//
// 3. Replace all three instance of the keyword `tempvar`
// with `local`, uncomment the line containing `alloc_locals`
// and run the code again.
//
// The behavior of the code doesn't change. This is because
// the keyword `local` allocates a new variables for the
// intermediate values as well. The main difference between
// `local` and `tempvar` is explained in the next challenge.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
// Outputs the value 2^9.
func main{output_ptr: felt*}() {
// alloc_locals;
tempvar x = 2;
let y = x * x * x;
let z = y * y * y;
// Because of the `let`s, the following line is equivalent to
// serialize_word(x * x * x * x * x * x * x * x * x).
serialize_word(z);
return ();
}
Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨
Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules
Newton
asked
6 months ago
-1
where are you from
0x0c44...AC36d0
answered
6 months ago
1
// Use the output builtin. %builtins output
// Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word
// Outputs the value 2^9.
func main{output_ptr: felt*}() {
alloc_locals;
local x = 2;
local y = x * x * x;
local z = y * y * y;
// Because of the let
s, the following line is equivalent to
// serialize_word(x * x * x * x * x * x * x * x * x).
serialize_word(z);
return ();
}
It decreased from having 15 steps, to 13 steps(even adding the "alloc_locals" step)
answered
6 months ago
0
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
// Outputs the value 2^9.
func main{output_ptr: felt*}() {
// alloc_locals;
let x = 2;
let y = x * x * x;
let z = y * y * y;
// Because of the `let`s, the following line is equivalent to
// serialize_word(x * x * x * x * x * x * x * x * x).
serialize_word(z);
return ();
}
Program output:
512
Number of steps: 7
Program hash: 0x01369567b8bbbd05c7bd71fc0f26943286f30246277672185a082e4c5d5476bb
Ishita Rastogi
answered
6 months ago
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
How to use get_fp_and_pc in Cairo Lang?
How to write a function in Cairo Lang (StarkNet)?
What is SHARP in Cairo Language?
How to make conditional expressions if..then..else in Cairo lang?
How to submit a StarkNet contract?
How to make output like "hello world" in Cairo Language (StarkNet)?
What do you think about current StarkNet Alpha and future Beta? What will Beta mean? When should it be beta?
How will work an account abstaction wallet with the key?
Will Kakarot be an L3 on top of the Starknet validity rollup?
How can I connect a StarkNet account using starknetjs?
How do I connect my DAPP to Starknet mainnet using starknetjs?
Any idea why this code doesn’t connect to argentX in brave?
What resources (Github repos, projects) do you follow about Cairo Language? Why?