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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:05:32+00:00 2026-06-16T03:05:32+00:00

I am carrying out the following modulo division operations from within a C program:

  • 0

I am carrying out the following modulo division operations from within a C program:

(5^6) mod 23 = 8

(5^15) mod 23 = 19

I am using the following function, for convenience:

int mod_func(int p, int g, int x) {
    return ((int)pow((double)g, (double)x)) % p;
}

But the result of the operations when calling the function is incorrect:

mod_func(23, 5, 6) //returns 8
mod_func(23, 5, 15) //returns -6

Does the modulo operator have some limit on the size of the operands?

  • 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-06-16T03:05:33+00:00Added an answer on June 16, 2026 at 3:05 am

    5 to the power 15 is 30,517,578,125

    The largest value you can store in an int is 2,147,483,647

    You could use 64-bit integers, but beware you’ll have precision issues when converting from double eventually.

    From memory, there is a rule from number theory about the calculation you are doing that means you don’t need to compute the full power expansion in order to determine the modulo result. But I could be wrong. Been too many years since I learned that stuff.

    Ahh, here it is: Modular Exponentiation

    Read that, and stop using double and pow =)

    int mod_func(int p, int g, int x)
    {
        int r = g;
        for( int i = 1; i < x; i++ ) {
            r = (r * g) % p;
        }
        return r;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been carrying out 2D and 3D operations, including graphics, for many years
Im carrying out some form validation with codeigniter using a custom validation callback. $this->form_validation->set_rules('testPost',
I am carrying out a number of calculations using NSDecimal and am creating each
I am carrying out a PDA version application from windows version. But found out
I am just carrying out a query, and have started using parameters. And all
How would you model a system that handles permissions for carrying out certain actions
I'm carrying out some experiments in theorem proving with combinator logic, which is looking
I am a student carrying out a study to enhance a search engine's existing
I am currently carrying out research for the design and implementation of a web-based
I have just recently started carrying out unit testing for my software written in

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.