Project Euler #16: Power digit sum

Sort by

recency

|

137 Discussions

|

  • + 0 comments

    in C++, I don't think there are a built in bigInteger like in Java or Python, so I made whatever I needed to make it work.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <sstream>
    
    constexpr size_t mod = 10000000000000000; // power of 10 relatively close to int64 max.
    
    void doubling(std::vector<size_t>& num)
    {
    	size_t ret = 0;
    	for (int i = 0; i < num.size(); i++)
    	{
    		size_t localNum = num[i];
    		localNum = localNum*2 + ret;
    		ret = localNum / mod;
    		num[i] = localNum % mod;
    	}
    	if (ret > 0)
    	{
    		num.push_back(ret);
    	}
    }
    
    // for each test case
    void run(int n)
    {
    	std::vector<size_t> num = { 1 };
    	for (int i = 0; i < n; i++)
    	{
    		doubling(num);
    	}
    
    	std::stringstream numStream;
    
    	for (int i = num.size() - 1; i >= 0; i--)
    	{
    		numStream << num[i];
    	}
    
    	std::string strnum = numStream.str();
    	size_t sum = 0;
    	for (char c : strnum)
    	{
    		sum += (static_cast<int>(c) - '0');
    	}
    
    	std::cout << sum << std::endl;
    
    }
    
    int main()
    {
    	int t;
    	std::cin >> t;
    	for (;t > 0; t--)
    	{
    		int n;
    		std::cin >> n;
    		run(n);
    	}
    	return 0;
    }
    
  • + 0 comments

    Java8

    public static int sumDigit(BigInteger n) {
            int count = 0;
            while (n.compareTo(BigInteger.ZERO) > 0) {
                count += n.mod(BigInteger.TEN).intValue(); 
                n = n.divide(BigInteger.TEN); 
            }
            return count;
        }
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int testcase = s.nextInt();
            for (int i = 0; i < testcase; i++) {
                int n = s.nextInt();
                BigInteger res = BigInteger.valueOf(2).pow(n); 
                System.out.println(sumDigit(res));
            }
            s.close();
        }
    
  • + 1 comment

    100% python

    for _ in range(int(input())):
        n = int(input())
        sn = str(2**n)
        ans = 0
        for d in sn:
            ans += int(d)
        print(ans)
    
  • + 1 comment

    Enter your code here. Read input from STDIN. Print output to STDOUT

    List = []

    if name == 'main': T = int(input())

    for i in range(T):
        raw_inputs = int(input())
        power = pow(2 , raw_inputs)
        List.append(power)
    
    
    
    
    
    for number in List:
        if number <= 9:
            print(number)
        else:
            digit_sum = sum(int(digit) for digit in str(number))
            print(digit_sum)
    
  • + 0 comments

    Python

    for _ in range(int(input())):
        n = int(input())
        sn = str(2**n)
        ans = 0
        for d in sn:
            ans += int(d)
        print(ans)