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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:22:23+00:00 2026-05-11T12:22:23+00:00

I’ve been wrestling with Project Euler Problem #16 in C# 2.0. The crux of

  • 0

I’ve been wrestling with Project Euler Problem #16 in C# 2.0. The crux of the question is that you have to calculate and then iterate through each digit in a number that is 604 digits long (or there-abouts). You then add up these digits to produce the answer.

This presents a problem: C# 2.0 doesn’t have a built-in datatype that can handle this sort of calculation precision. I could use a 3rd party library, but that would defeat the purpose of attempting to solve it programmatically without external libraries. I can solve it in Perl; but I’m trying to solve it in C# 2.0 (I’ll attempt to use C# 3.0 in my next run-through of the Project Euler questions).

Question

What suggestions (not answers!) do you have for solving project Euler #16 in C# 2.0? What methods would work?

NB: If you decide to post an answer, please prefix your attempt with a blockquote that has ###Spoiler written before it.

  • 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. 2026-05-11T12:22:24+00:00Added an answer on May 11, 2026 at 12:22 pm

    A number of a series of digits. A 32 bit unsigned int is 32 binary digits. The string ‘12345’ is a series of 5 digits. Digits can be stored in many ways: as bits, characters, array elements and so on. The largest ‘native’ datatype in C# with complete precision is probably the decimal type (128 bits, 28-29 digits). Just choose your own method of storing digits that allows you to store much bigger numbers.

    As for the rest, this will give you a clue:

    21 = 2
    22 = 21 + 21
    23 = 22 + 22

    Example:

    The sum of digits of 2^100000 is 135178 Ran in 4875 ms  The sum of digits of 2^10000 is 13561 Ran in 51 ms  The sum of digits of 2^1000 is 1366 Ran in 2 ms 

    SPOILER ALERT: Algorithm and solution in C# follows.

    Basically, as alluded to a number is nothing more than an array of digits. This can be represented easily in two ways:

    • As a string;
    • As an array of characters or digits.

    As others have mentioned, storing the digits in reverse order is actually advisable. It makes the calculations much easier. I tried both of the above methods. I found strings and the character arithmetic irritating (it’s easier in C/C++; the syntax is just plain annoying in C#).

    The first thing to note is that you can do this with one array. You don’t need to allocate more storage at each iteration. As mentioned you can find a power of 2 by doubling the previous power of 2. So you can find 21000 by doubling 1 one thousand times. The doubling can be done in place with the general algorithm:

    carry = 0 foreach digit in array   sum = digit + digit + carry   if sum > 10 then     carry = 1     sum -= 10   else     carry = 0   end if   digit = sum end foreach 

    This algorithm is basically the same for using a string or an array. At the end you just add up the digits. A naive implementation might add the results into a new array or string with each iteration. Bad idea. Really slows it down. As mentioned, it can be done in place.

    But how large should the array be? Well that’s easy too. Mathematically you can convert 2^a to 10^f(a) where f(a) is a simple logarithmic conversion and the number of digits you need is the next higher integer from that power of 10. For simplicity, you can just use:

    digits required = ceil(power of 2 / 3) 

    which is a close approximation and sufficient.

    Where you can really optimise this is by using larger digits. A 32 bit signed int can store a number between +/- 2 billion (approximately. Well 9 digits equals a billion so you can use a 32 bit int (signed or unsigned) as basically a base one billion ‘digit’. You can work out how many ints you need, create that array and that’s all the storage you need to run the entire algorithm (being 130ish bytes) with everything being done in place.

    Solution follows (in fairly rough C#):

        static void problem16a()     {         const int limit = 1000;         int ints = limit / 29;         int[] number = new int[ints + 1];         number[0] = 2;         for (int i = 2; i <= limit; i++)         {             doubleNumber(number);         }         String text = NumberToString(number);         Console.WriteLine(text);         Console.WriteLine('The sum of digits of 2^' + limit + ' is ' + sumDigits(text));     }      static void doubleNumber(int[] n)     {         int carry = 0;         for (int i = 0; i < n.Length; i++)         {             n[i] <<= 1;             n[i] += carry;             if (n[i] >= 1000000000)             {                 carry = 1;                 n[i] -= 1000000000;             }             else             {                 carry = 0;             }         }     }      static String NumberToString(int[] n)     {         int i = n.Length;         while (i > 0 && n[--i] == 0)             ;         String ret = '' + n[i--];         while (i >= 0)         {             ret += String.Format('{0:000000000}', n[i--]);         }         return ret;     } 
    • 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.