Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

Can someone help me write a non-recursive version if this function in C? I just started learning C after Scheme and loops are still kind of confusing for me.

Computer Science
I got my questions answered at in under 10 minutes. Go to now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer


To see the expert answer you'll need to create a free account at Brainly

The code doesn't look like c. if it is which version or type is it?
If you can describe the functionality I can help
Ok is it computing \[x^{e} \mod m\] And you can write a recursive C version.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

int xsqrm( int x, int m) { return (x * x) % m int powermod2(int x, int e, int m) { if (e == 0) return 1; if (e % 2 == 0) return powermod2(xsqrm(x, m), e/2); return (x * powermod2(xsqrm(x, m), e/2)) % m ; }
for anyone curious this is the solution I came up with int powmod2(int x, int e, int m) { int y = 1; while (e!= 0) { if (e%2 == 0) { e = e/2; x = (x*x)%m; } else { e = (e-1)/2; y = (x*y)%m; x = (x*x)%m; } } return y; } My problem was that I was missing a step in the odd case that I was able to introduce with the y variable. The original I needed to convert this was a recursive version written in scheme. Thanks for your help though!
e = e/2 is enough in both cases; because integer division truncates. So you can remove the two statements inside the if and else and replace by e /= 2 in the while loop

Not the answer you are looking for?

Search for more explanations.

Ask your own question