// 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
//
// What is the sum of the digits of the number 2^1000?
using System;
using System.Collections.Generic;
using System.Text;
namespace projecteuler016
{
class Program
{
/// <summary>
/// 每个数字在2^n中的个数
/// </summary>
static int[] x = new int[10];
static void Main(string[] args)
{
F1();
}
private static void F1()
{
Console.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod());
DateTime timeStart = DateTime.Now;
/*
for (int i = 0; i < 100; i++)
{
long p = (long)Math.Pow(2, i);
Console.WriteLine(i + ":" + p + ":" + digitTotal(p) + ":" + y(p));
}*/
/*
for (int i = 1; i < 100; i++)
{
Console.WriteLine(y(i));
}*/
/*
x = xn(1);
for (int i = 0; i < 10; i++)
{
Console.WriteLine(x[i]);
}*/
string result="1";
int sum=0;
for (int i=0;i<1000;i++)
{
result=longAdd(result,result,0);
}
for (int i = result.Length - 1; i >= 0;i-- )
{
sum += result[i] - '0';
}
Console.WriteLine("2^1000 = "+result);
Console.WriteLine("The sum of these digits is " + sum);
Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "\n\n");
}
private static long digitTotal(long value)
{
int total = 0;
while (value > 0)
{
total += (int)(value % 10);
value /= 10;
}
return total;
}
private static int y(long value)
{
/*
int total = 0;
while (value > 0)
{
if (value % 10 >= 5)
{
total++;
}
value /= 10;
}
return total;*/
int[] x = xn(value);
int result = 0;
for (int i = 5; i < 10; i++)
{
result += x[i];
}
return result;
}
private static int[] xn(long n)
{
int[] result = new int[10];
if (n == 0)
{
result[1] = 1;
return result;
}
int[] lastN = xn(n - 1);
for (int i = 0; i < 10; i++)
{
if (i < 5)
{
result[2 * i] = lastN[i] + lastN[2 * i];
}
else
{
result[2 * i - 10] = lastN[i] + lastN[2 * i - 10];
result[1]++;
}
}
return result;
}
/// <summary>
/// 超大数字表达式加法
/// </summary>
/// <param name="s1">数1</param>
/// <param name="s2">数2</param>
/// <param name="c">后面的数的进位,范围0~2</param>
/// <returns></returns>
private static string longAdd(string s1, string s2, int c)
{
//s1,s2的末位Index
int l1 = s1.Length - 1;
int l2 = s2.Length - 1;
int x;
//如果2个数都不为空
if (l1 >= 0 && l2 >= 0)
{
x = s1[l1] - '0' + s2[l2] - '0' + c;
return longAdd(s1.Substring(0, l1), s2.Substring(0, l2), x / 10) + (x % 10).ToString();
}
//下面的情况,s1和s2中至少有一个为空,我们要做的是找到那个不为空的进行下一步操作
//把不为空的值放到s1里
if (l2 >= 0)
{
s1 = s2;
l1 = l2;
}
else if (l1 == -1) //表示全为空,判断最后的进位,如果没进位,返回空白,结束递归
{
if (c!=0)
{
return c.ToString();
}
return "";
}
x = s1[l1] - '0' + c;
//如果s1只有1位
if (l1 == 0)
{
return x.ToString();
}
//如果s1有不止一位
return longAdd(s1.Substring(0, l1), "", x / 10) + (x % 10).ToString();
}
}
}
/*
Void F1()
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510
51124936122493198378815695858127594672917553146825187145285692314043598457757469
85748039345677748242309854210746050623711418779541821530464749835819412673987675
59165543946077062914571196477686542167660429831652624386837205668069376
The sum of these digits is 1366
Total Milliseconds is 166.021
By GodMoon
*/
【ProjectEuler】ProjectEuler_016
最新推荐文章于 2018-10-15 10:56:54 发布