NEWTON

NEWTON


Popular tags

    How to divide felt in Cairo Lang using unsigned_div_rem?

    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

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    3 months ago


    2 answers

    0

    Accepted answer

    Solution

    %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 ();
    }
    
    

    Output

    Program output:
      4
      0
      4
      1
    
    Number of steps: 70
    Program hash: 0x0238e10075bbd2ad40c6275fc85e62ffd27a649bd13415ae8b74a5ff49647509
    

    Explanation

    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

    Your answer

    NEWTON

    NEWTON