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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:59:26+00:00 2026-05-16T02:59:26+00:00

I’m working on Project Euler to brush up on my C++ coding skills in

  • 0

I’m working on Project Euler to brush up on my C++ coding skills in preparation for the programming challenge(s) we’ll be having this next semester (since they don’t let us use Python, boo!).

I’m on #16, and I’m trying to find a way to keep real precision for 2¹°°°

For instance:

int main(){
    double num = pow(2, 1000);
    printf("%.0f", num):
    return 0;
}

prints

10715086071862673209484250490600018105614050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Which is missing most of the numbers (from python):

>>> 2**1000

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376L

Granted, I can write the program with a Python 1 liner

sum(int(_) for _ in str(2**1000))

that gives me the result immediately, but I’m trying to find a way to do it in C++. Any pointers? (haha…)

Edit:

Something outside the standard libs is worthless to me – only dead-tree code is allowed in those contests, and I’m probably not going to print out 10,000 lines of external code…

  • 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-16T02:59:27+00:00Added an answer on May 16, 2026 at 2:59 am

    If you just keep track of each digit in a char array, this is easy. Doubling a digit is trivial, and if the result is greater than 10 you just subtract 10 and add a carry to the next digit. Start with a value of 1, loop over the doubling function 1000 times, and you’re done. You can predict the number of digits you’ll need with ceil(1000*log(2)/log(10)), or just add them dynamically.

    Spoiler alert: it appears I have to show the code before anyone will believe me. This is a simple implementation of a bignum with two functions, Double and Display. I didn’t make it a class in the interest of simplicity. The digits are stored in a little-endian format, with the least significant digit first.

    typedef std::vector<char> bignum;
    
    void Double(bignum & num)
    {
        int carry = 0;
        for (bignum::iterator p = num.begin();  p != num.end();  ++p)
        {
            *p *= 2;
            *p += carry;
            carry = (*p >= 10);
            *p -= carry * 10;
        }
        if (carry != 0)
            num.push_back(carry);
    }
    
    void Display(bignum & num)
    {
        for (bignum::reverse_iterator p = num.rbegin();  p != num.rend();  ++p)
            std::cout << static_cast<int>(*p);
    }
    
    int main(int argc, char* argv[])
    {
        bignum num;
        num.push_back(1);
        for (int i = 0;  i < 1000;  ++i)
            Double(num);
        Display(num);
        std::cout << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.