NEWTON

Asked

6 months ago

127

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*

- #17daysOfCairocairo-beginnerscairoplayground

N

Newton

asked

6 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`

IR

Ishita Rastogi

answered

6 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

6 months ago

7

How to make output like "hello world" in Cairo Language (StarkNet)?

0

How to make math operation with Field Elements (felts) in Cairo lang?

0

How to use get_fp_and_pc in Cairo Lang?

3

How to write a function in Cairo Lang (StarkNet)?

1

How to submit a StarkNet contract?

0

Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?

0

How to make recursive function in Cairo Lang?

0

How to transpile multiple contracts at once using warp?

0

StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT error in Cairo. Can anyone help?

0

Status “received” while deploying starknet transaction. Is it ok?

0

What Implicit arguments are in Cairo lang?

0

Which is this extension that shows you errors before compilation ser?

0

Cairo: Getting the squared summation of felts in an array

0

Error when deploying Cairo: [WARNING] alpha-goerli is a legacy network name parameter and it won't be supported in future versions