Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

bluebrandon

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

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. DBhatta
    Best Response
    You've already chosen the best response.
    Medals 0

    The code doesn't look like c. if it is which version or type is it?

    • 2 years ago
  2. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 2

    If you can describe the functionality I can help

    • 2 years ago
  3. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 2

    Ok is it computing \[x^{e} \mod m\] And you can write a recursive C version.

    • 2 years ago
  4. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 2

    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 ; }

    • 2 years ago
  5. bluebrandon
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

    • 2 years ago
  6. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 2

    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

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.