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 796459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:38:34+00:00 2026-05-14T22:38:34+00:00

I raise some basis b to the power p and take the modulo m

  • 0

I raise some basis b to the power p and take the modulo m of that.

Let’s assume b=55170 or 55172 and m=3043839241 (which happens to be the square of 55171). The linux-calculator bc gives the results (we need this for control):

echo "p=5606;b=55171;m=b*b;((b-1)^p)%m;((b+1)^p)%m" | bc
2734550616
309288627

Now calculating 55170^5606 gives a somewhat large number, but since I have to do a modulooperation, I can circumvent the usage of BigInt, I thought, because of:

(a*b) % c == ((a%c) * (b%c))%c i.e.
(9*7) % 5 == ((9%5) * (7%5))%5 =>
63 % 5    == (4     *    2) %5 =>
3         == 8 % 5

… and a^d = a^(b+c) = a^b * a^c, therefore I can divide b+c by 2, which gives, for even or odd ds d/2 and d-(d/2), so for 8^5 I can calculate 8^2 * 8^3.

So my (defective) method, which always cut’s off the divisor on the fly looks like that:

def powMod (b: Long, pot: Int, mod: Long) : Long = { 
      if (pot == 1) b % mod else {
          val pot2 = pot/2
          val pm1 = powMod (b, pot2, mod)             
          val pm2 = powMod (b, pot-pot2, mod)           
          (pm1 * pm2) % mod 
      } 
}

and feeded with some values,

powMod (55170, 5606, 3043839241L) 
res2: Long = 1885539617
powMod (55172, 5606, 3043839241L) 
res4: Long = 309288627

As we can see, the second result is exactly the same as the one above, but the first one looks quiet different. I’m doing a lot of such calculations, and they seem to be accurate as long as they stay in the range of Int, but I can’t see any error. Using a BigInt works as well, but is way too slow:

def calc2 (n: Int, pri: Long) = {
    val p: BigInt = pri
    val p3 = p * p
    val p1 = (p-1).pow (n) % (p3)
    val p2 = (p+1).pow (n) % (p3)
    print ("p1: " + p1 + " p2: " + p2)
}

calc2 (5606, 55171) 
p1: 2734550616 p2: 309288627

(same result as with bc) Can somebody see the error in powMod?

  • 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-14T22:38:34+00:00Added an answer on May 14, 2026 at 10:38 pm

    I think the answer is here:

    scala> math.sqrt(Long.MaxValue).toLong < 3043839241L
    res9: Boolean = true
    

    That means you can have a long overflow even for numbers which are less than that particular module value. Let’s try to catch it:

    scala> def powMod (b: Long, pot: Int, mod: Long) : Long = {
         |       if (pot == 1) b % mod else {
         |           val pot2 = pot/2
         |           val pm1 = powMod (b, pot2, mod)
         |           val pm2 = powMod (b, pot-pot2, mod)
         |           val partial = ((pm1 % mod) * (pm2 % mod)).ensuring(res =>
         |             res > pm1 % mod && res > pm2 % mod, "Long overflow multiplying "+pm1+" by "+pm2)
         |           partial % mod
         |       }
         | }
    powMod: (b: Long,pot: Int,mod: Long)Long
    
    scala> powMod (55170, 5606, 3043839241L)
    java.lang.AssertionError: assertion failed: Long overflow multiplying 3042625480 by 3042625480
    

    There you have it.

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

Sidebar

Related Questions

I have some code that raises PropertyChanged events and I would like to be
How do I raise an event from a user control that was created dynamically?
I understand that one can raise an event in the class that the implementation
I have a static class that I would like to raise an event as
Is there a good way to find out which exceptions a procedure/function can raise
Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
How do you get two unrelated controls to raise the same custom event? All
I am pretty sure the following button-activated form code should raise a Control-F12 in
INotifyPropertyChanged is fairly self explanatory and I think I'm clear on when to raise
Evil or not evil? public static void Raise(this EventHandler handler, object sender, EventArgs args)

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.