Hey guys! My professor proposed me to solve this problme. Given a number p <= 10^7, print the sum of the digits of 2^p. For exemple, 2^4 = 16 so we'll print 7. How can I do this properly? I first tried to use in a way or in another, the little theorem of fermat but it made no sense here.
Well since 2p will have less than 107 digits you can compute 2p by using FFT with complexity Nlog2N. Then you simply find the sum of the digits.
So this is the only solution. I was wondering if there is a solution with math for every number not only 2
2^10^7 has 3010300 decimal digits which is greater than 10^7 then how can you say that 2^p is less than 10^7 ??
3010300 has only 7 digits, how can it be larger than 10^7?
i didn't mean it. 2^10^7 has 3010300 digits in decimal representation.
10107 will have exactly 107+1 digits and since 2<10 then 2107 will have less than 107+1 digits.
Even better. Realize that 1log1 + 2log2 + 4log4 + 8log8 + ... + nlogn is O(nlogn), thus your solution is O(nlogn).
Can you please explain, why is this formula correct? Something is very wrong about it.
N + N/2 + N/4 + N/8 + N/16 + ... converges to 2N
Well, yeah, this one is true, but the formula must be ∑ni=1n/i⋅i⋅log(i) for all powers of two which is O(n⋅log2). You can even apply master theorem here f(n)=2f(n/2)+O(nlog)=O(n⋅log2)
No, it's simple. All logs in that sum are at most logN, so you can estimate that it's ≤1logN+2logN+4logN+…+(N/2)logN+NlogN =logN(1+2+4+…+N/2+N).
But it's not 1+2+4+...+n, because our sum is ∑n/i⋅i⋅log(i)=n∑log(i)≥n⋅(log(n)/2)2
What do the terms in that sum correspond to? It clearly can't be computing 21,22,23,… step by step because each term is at least O(N).
My algorithm is a little different from binary exponentiation. It can multiply any n short numbers, so it takes nlog2. And yes, binary exponentiation will take exactly nlog time.
Ok, so now you know that multiplying many small numbers isn't a great idea.
You don't need to do two recursive calls to do binary exponentiation.
Using your logic plain binary exponentiation works in f(n)=2f(n/2)+O(1)=O(n)
Wait, that's illegal.
How is it possible you can multiply two numbers of length n/2 in O(1). I think the reccurence must be f(n)=f(n/2)+O(nlog)=O(nlog).
or just do plain of iterative exp
both of these are exactly the recurrence you said.
Tell your prof that in base 2 the sum of the digits is just 1
Your answer seems so straight forward that I spent few minutes thinking how to compute sum of digits mod 2, just to realize that you mean't sum of digits when the number is itself in base 2 :facepalm:
Is it any easy way to compute sum of digits mod 2?
interesting, i like your prof
you can do the computation in strings then finally add the result
Hey why the downvotes? This a totally valid solution since the question didn't specify required complexity or time limit.
Do we need to specify every time that time limit is smaller than a few hours?
"This a totally valid solution" — excuse me, but where do you see a solution here? Information of how to store the digits doesn't seem to me like a totally valid solution :).
Lol he suggested manually calculating it with biginteger strings, which, while dumb, is a solution.
actually yes it tried to do it and it gives tle.