[examples] update field twoadicity example with @program and calling helper function

This commit is contained in:
Eric McCarthy 2022-08-05 22:48:43 -07:00
parent 57984afb26
commit c54740cc5e

View File

@ -1,11 +1,12 @@
// The 'twoadicity' main function.
@program
function main(public n: field) -> u8 {
let remaining_n: field = n;
let powers_of_two: u8 = 0u8;
// Since field ints are 253 bits or fewer,
// any number in the field will have at most 252 powers of two in its prime factoring.
for i:u8 in 0u8..252u8 {
if (remaining_n / 2field < remaining_n) {
if is_even_and_nonzero(remaining_n) {
remaining_n = remaining_n / 2field;
powers_of_two = powers_of_two + 1u8;
}
@ -21,8 +22,6 @@ function main(public n: field) -> u8 {
If we add p to both of these negative numbers, we have
n/2 = (n-p)/2 + p = (n+p)/2 is greater than n and still less than p.
*/
/* We don't yet have function calls, so this got inlined above.
function is_even_and_nonzero (n: field) -> bool {
return n / 2field < n;
}
*/