-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathques-6.cairo
42 lines (40 loc) · 1.34 KB
/
ques-6.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
%lang starknet
from starkware.cairo.common.math import assert_nn
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.find_element import find_element
from starkware.cairo.common.uint256 import (
Uint256,
uint256_eq,
uint256_le,
uint256_lt,
uint256_signed_div_rem,
)
from starkware.cairo.common.math import assert_nn_le, split_felt, unsigned_div_rem
from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.dict import dict_read, dict_write
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.math_cmp import is_le
@view
func count_digit{syscall_ptr : felt*, range_check_ptr, pedersen_ptr : HashBuiltin*}(
number, digit, base
) -> (amount):
if number == digit:
return (1)
end
return count_digit_util(number, digit, base, 0)
end
@view
func count_digit_util{syscall_ptr : felt*, range_check_ptr, pedersen_ptr : HashBuiltin*}(
number, digit, base, counter
) -> (amount):
let (q, r) = unsigned_div_rem(number, base)
if q != 0:
if r == digit:
return count_digit_util(q, digit, base, counter + 1)
end
return count_digit_util(q, digit, base, counter)
end
return (counter)
end