NEWTON
Asked
3 months ago
118
views
0
Hello! This is day 9 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Field Elements 2”. Perhaps you can help me?
// As you saw in the previous challenge, the operator '/' does not denote
// truncated (i.e., rounding down) integer division as in other programming
// languages. Instead, one can use library functions such as
// unsigned_div_rem().
//
// Note that unsigned_div_rem() does not work for negative numbers.
%builtins output range_check
// Import unsigned_div_rem() from the math module.
from starkware.cairo.common.math import unsigned_div_rem
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*, range_check_ptr}() {
let (q, r) = unsigned_div_rem(value=12, div=3);
// Output the quotient (12 // 3).
serialize_word(q);
// Output the remainder (12 % 3).
serialize_word(r);
// Add code that outputs the quotient and remainder of 13 / 3 in addition to 12 / 3 here.
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
3 months ago
0
Accepted answer
%builtins output range_check
// Import unsigned_div_rem() from the math module.
from starkware.cairo.common.math import unsigned_div_rem
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*, range_check_ptr}() {
let (q, r) = unsigned_div_rem(value=12, div=3);
// Output the quotient (12// 3).
serialize_word(q);
// Output the remainder (12 % 3).
serialize_word(r);
// Add code that outputs the quotient and remainder of 13 / 3 in addition to 12 / 3 here.
let (a, b) = unsigned_div_rem(value=13, div=3);
// Output the quotient (13// 3).
serialize_word(a);
// Output the remainder (13 % 3).
serialize_word(b);
return ();
}
Program output:
4
0
4
1
Number of steps: 70
Program hash: 0x0238e10075bbd2ad40c6275fc85e62ffd27a649bd13415ae8b74a5ff49647509
unsigned_div_rem function signature looks like this:
func unsigned_div_rem{range_check_ptr}(value, div) -> (q : felt, r : felt):
It accepts two argument value and div. The value of div is restricted to make sure there is no overflow and it returns quotient and remainder such that:
0 <= q < rc_bound, 0 <= r < div and value = q * div + r
Ishita Rastogi
answered
3 months ago
0
Here is my solution for today's challenge
%builtins output range_check
// Import unsigned_div_rem() from the math module.
from starkware.cairo.common.math import unsigned_div_rem
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*, range_check_ptr}() {
let (q, r) = unsigned_div_rem(value=12, div=3);
// Output the quotient (12 // 3).
serialize_word(q);
// Output the remainder (12 % 3).
serialize_word(r);
// Add code that outputs the quotient and remainder of 13 / 3 in addition to 12 / 3 here.
let (q2,r2) = unsigned_div_rem(value=13, div=3);
serialize_word(q2);
serialize_word(r2);
return ();
}
answered
3 months ago
How to make output like "hello world" in Cairo Language (StarkNet)?
How to make math operation with Field Elements (felts) in Cairo lang?
How to use get_fp_and_pc in Cairo Lang?
How to write a function in Cairo Lang (StarkNet)?
How to submit a StarkNet contract?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
How to make recursive function in Cairo Lang?
Fixed Point pow operation error
A TypeError while unpacking nested tuples into felts
What will the bundler ecosystem look like in Account Abstraction?
storage for array
Issue with generating _abi.json file
StarknetPy Error in get_balance_sync (StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT)
How to verify Empiric’s Data Entries Using Events?