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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:35:23+00:00 2026-06-07T21:35:23+00:00

I am writing a function that is supposed to do powers, but only display

  • 0

I am writing a function that is supposed to do powers, but only display the last n digits specified. It is working great… mostly. For some reason when I specify how many of the last digits I want, it works fine all the way up to and including the number 12. Any digit amount above 12 seems to give me a very strange number. I know I must be missing something obvious, but I really don’t see it.

Here is the code:

function power(base, exponent, digits) {
total = base;
for(i = 1; i < exponent; i++) {
    total =  total * base;

    if(total.toString().length > digits)  {
        total = total.toString().substr(total.toString().length - digits, digits);
        }
    }

return total;
}

So, for some examples (displaying 12 digits and lower works fine):

If I do power(999, 999, 1) I end up with => 9

If I do power(999, 999, 5) I end up with => 98999

If I do power(999, 999, 12) I end up with => 000499998999

Here is where it starts messing up:

If I do power(999, 999, 13) I end up with => 5710054009000

If I do power(999, 999, 14) I end up with => ‘79077027006000’

At first I thought I was hitting some kind of integer limit and scientific notation was screwing things up, but I don’t think that is the case, because it should work up to 20 digits.

I suspect it is something wrong with the way I am reducing the string in the if statement. But I am not sure why it wouldn’t mess up for calculations below 13 digits.

Thanks!

  • 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-07T21:35:25+00:00Added an answer on June 7, 2026 at 9:35 pm

    You need a function to compute b^e (mod m). A good algorithm is known as square and multiply:

    Algorithm PowerMod: Compute b^e (mod m) with b, e and m all positive integers.
    1. [Initialize] Set r := 1.
    2. [Terminate when finished] If e == 0, return r and stop.
    3. [Multiply if odd] If e is odd, set r := r * b (mod n).
    4. [Square and iterate] Set e := floor(e / 2). Set b := b * b (mod n). Go to Step 2.
    

    Then to make your computation, say PowerMod(999, 999, 10^14); you should get 17000499998999.

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

Sidebar

Related Questions

I'm working in a CakePHP Model class, writing a function that is supposed to
I'm writing a recursive function in Ocaml that's supposed to count the number of
I've had trouble writing this code. I'm supposed to make a function that can
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a meta function replace_type<C, X, Y> that is supposed to replace
I am writing a program that is meant to be extended by some function
i am writing pl/pgsql function that does some statistics processing. using 8.2 of postgres.
I am writing function that involve other function from base R with a lot
I'm writing a function that replaces long hex coded color ( #334455 ) with
I am writing a function that takes in a output target file and a

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.