Algorithm, Data structure/Solved Algorithmic Problem

Project Euler #16 - Power digit sum

JaykayChoi 2016. 6. 21. 01:00

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?



출처: https://projecteuler.net/


큰 수의 곱셈은 karatsuba algorithm 을 통해 구했고 2 power 1000 은 divide and conquer algorithm 을 통해 시간 복잡도를 줄였습니다. multiply 메써드는 karatsuba algorithm 포스팅에서 확인하실 수 있습니다.




my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
 
string solve(int n)
{
    if (n == 1)
        return "2";
    if (n % == 1)
        return multiply(solve(n - 1), string("2"));
 
    return multiply(solve(n / 2), solve(n / 2));
}
 
int main()
{
    string str = solve(1000);
 
    int ret = 0;
    for (int i = 0; i < str.size(); i++)
        ret += (str[i] - '0');
 
    cout << ret << endl;
 
    system("pause");
    return 0;
}
cs

multiply method is in karatsuba algorithm posting