此方法采用模拟手动计算乘法的方法
#include<stdio.h>
#include<stdlib.h>
void main()
{
int sum = 0;//用来统计计算结果各位之和
int count = 1;//用来记录strs数组被用了几位
int carry = 0;//carry用来保存进位
int i;
int strs = { 1 };//定义一个长度为1000的整型数组,并将strs初始化为1,方便运算
for (i = 0; i < 1000; i++)//第一层循环保证每次结果乘以2,乘以1000次,即2^1000
{
for (int j = 0; j < count; j++)//对已经使用的位,每次乘以2,手动模拟乘法
{
strs = strs * 2 + carry;//对数组的每一位处理,乘以2再加上进位
carry = strs / 10;//处理进位
strs %= 10;//每一位是一个0-9的数字
}
while (carry > 0)//上一个for循环结束,最高位可能还会产生进位,需要处理
{
strs = carry;//先将进位保存在数组
if (strs > 9)//进位大于9,还需要进一步处理
{
carry = strs / 10;//处理与上述模拟一样
strs %= 10;
}
else
{
carry = 0;//不产生了进位,将进位置为0,以免产生影响
}
}
}
printf("%d\n", count);//输出使用了多少位
printf("2^1000的结果为:\n");
for (i = count-1; i >= 0; i--)//从高位到低位输出结果
{
printf("%d", strs);
}
printf("\n");
for (i = count - 1; i >= 0; i--)//计算各位之和
sum += strs;
printf("2^1000的各位数字之和 = %d\n",sum);//输出各位之和的结果
system("pause");//暂停一下
}
输出:
302
2^1000的结果为:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
2^1000的各位数字之和 = 1366
'''2的1000次方的各位数加起来是多少?'''
def sum_of_numbers(power):
number = 2 ** power
number_seqeuences = []
sum = 0
for each in str(number):
number_seqeuences.append(each)
for each in number_seqeuences:
sum += int(each)
print(number_seqeuences)
print(sum)
start_sum_of_numbers = time.time()
sum_of_numbers(1000)
time_sum_of_numbers = time.time() - start_sum_of_numbers
print("%f秒" %time_sum_of_numbers)
1366
0.000000秒
import math as m
def sum_pow(n):
a = int (m.pow(2,n))
a = str(a)
count = 0
for each in a:
i = int(each)
count = count + i
return count
n = 1000
print(sum_pow(n))
JonTargaryen 发表于 2017-10-10 14:51
也不能说你们的答案不对,但是感觉欧拉计划里面的很多问题如果用Python就太简单了,尤其是当涉及到大数的时 ...
说的好, 太多了 本帖最后由 a1351468657 于 2021-3-8 09:53 编辑
#include <stdio.h>
#include <string.h>
#include <math.h>
main()
{
int num = pow(2, 20);
char str, temp;
int len, i, j, k, a, b, c = 0;
itoa(num, str, 10);
for (i = 0; i < 980; i++)
{
len = strlen(str);
for (j = len - 1, k = 0; j >= 0; j--, k++)
{
a = str - '0';
a *= 2;
b = (a + c) % 10;
c = a / 10;
temp = b + '0';
}
while (c)
{
temp = c % 10 + '0';
c /= 10;
k++;
}
temp = '\0';
for (j = k - 1, c = 0; j >= 0; j--, c++)
{
str = temp;
}
str = '\0';
c = 0;
}
len = strlen(str);
k = 0;
for (i = 0; i < len; i++)
{
j = str - '0';
k += j;
}
printf("\n%d\n", k);
}
答案:1366
若有改进的地方望大佬指出! total = str(2 ** 1000)
sum = 0
for i in total:
sum += int(i)
print(sum) #include<stdio.h>
#include<math.h>
#define Num(pow(2,1000))
int main(void)
{
int sum = 0;
for(int i = 1;i < Num;i+=10)
{
sum += (Num / (i *10)) % i;
}
printf("%d",sum);
return 0;
}
import time as t
start = t.perf_counter()
print(sum())
print("It costs %f s" % (t.perf_counter() - start))
use num::bigint::ToBigUint;
use std::time::Instant;
fn main() {
let now = Instant::now();
let num: i32 = 2
.to_biguint()
.unwrap()
.pow(1000)
.to_string()
.bytes()
.collect::<Vec<_>>()
.into_iter()
.map(|x| (x - 48) as i32)
.sum();
println!("cost {} ms.", now.elapsed().as_millis());
println!("{num}")
}
---
cost 0 ms.
1366 我爱死c++了。^_^(呵呵) #include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i,k,l,m=0;
int sum=0;
int array={0};
array=1;
for(i=1;i<=1000;i++)//完成2的1000次方
{
for(k=0;k<1000;k++)//完成对一个数的*2
{
array=array*2;
}
for(l=0;l<=1000;l++)//检验从个位到所有可能要进1的位
{
if(array>=10) {
array=array%10;
++array;
}
}
}
for(i=0;i<1000;i++)
{
sum=array+sum;
}
printf("2的1000次方的各位之和是:%d\n",sum);
return 0;
}
页:
1
[2]