Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:13:02+00:00 2026-05-26T03:13:02+00:00

My homework was to provide a function that computes ‘x^y mod n’ -for any

  • 0

My homework was to provide a function that computes ‘x^y mod n’ -for any n < (sqrt maxint32)

So I started by writing doing this:

modPow :: Int -> Int -> Int -> Int 
modPow x y n = (x `mod` n) ^ (y `mod` n) `mod` n

Which seemed to work fine, for any number of n, although my next homework question involved using x^n mod n = x (Camichael numbers) and I could never get modPow to work.

So I made another modPow using pseudocode for mod exponentiation, -from wikipedia:

modPow2 :: Int -> Int -> Int -> Int 
modPow2 x y n 
 = loopmod 1 1
  where
   loopmod count total = if count > y
                          then total
                           else loopmod (count+1) ((total*x) `mod` n)

Which now correctly produces the right answer for my next question, (x^n mod n = x) -for checking for Camichael numbers.

ALTHOUGH, modPow2 does not work for big numbers of ‘y’ (STACK-OVERFLOW!!)

How could I adjust modPow2 so it no longer gets a stackoverflow in the cases where y > 10,000 (but still less than sqrt of maxint 32 -which is around 46,000)

Or is there a fix on my original modPow so it works with x^n mod n = x? (I always do 560 561 561 as inputs and it gives me back 1 not 560 (561 is a carmichael number so should give 560 back)

Thanks alot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:13:02+00:00Added an answer on May 26, 2026 at 3:13 am

    This is probably because the argument total is computed lazily.

    If you use GHC, you can make loopmod strict in total by placing a ! in frontof the argument, i.e.

    loopmod count !total = ...
    

    Another way would be to force evaluation of total like so: Replace the last line with

    else if total == 0 then 0 else loopmod (count+1) ((total*x) `mod` n)
    

    This does not change semantics (because 0*xis 0 anyway, so the reminder must be 0 also) and it forces hugs to evaluate total in every recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on this homework that's kind of confusing me... I am provided with
For homework, I am writing a program that deals with a lot of time_t
I am doing a homework assignment that reads in a book. First, a line
Hi I'm pretty new to Stack Overflow so I hope that I'm doing this
I want to preface this by saying that this is a homework assignment. I
(This is no homework and no work issue. It's just my personal interest/occupation and
For a homework assignment I was given a Card class that has enumerated types
I'm doing some homework and while I have some experience with SML, Haskell has
This is homework, and this questions Extends this one So there is a button
This is homework. Beginning Java class. Still wrapping my head around this stuff. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.