2

How to calculate the sum of (1+a%m+a^2%m……+a^n%m) where m=k!, 1<=k<=12, n<=10^18. How to Calculate this sum. Using computer and the time limit is 3 sec. Sorry about my mistake

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • 5
    This question appears to be off-topic because it is a maths question. – Ben Sep 23 '13 at 09:42
  • `m=k!.1<=k<=12.n<=10^1` ?? – Grijesh Chauhan Sep 23 '13 at 09:44
  • 2
    in a for(i=1; i<=n; i++) loop? ;-) – Sam Holder Sep 23 '13 at 09:44
  • mathoverflow Mathemetic Mathematics ...Please check these stackexchange sites,And the tag gives me you are asking for algorithm .Make it in the Question title or in the @n content that you are asking the algorithm – Lithu T.V Sep 23 '13 at 09:45
  • This would be a ***very*** simple program/algorithm (see comment from @SamHolder, above), is that really what you want help with? Or is there some additional/unobvious issue here? – RBarryYoung Sep 23 '13 at 09:46
  • 1
    Hint `(a+b)%m = (a%m +b%m)%m` this way you can reduce the summands. Than think about, if you really need to calculate each summand. – MrSmith42 Sep 23 '13 at 09:47
  • @RBarryYoung: One obvious issue is that the problem, as stated, requires adding up to 1,000,000,000,000,000,001 numbers in under 3 seconds. :-) – NPE Sep 23 '13 at 09:48
  • @NPE Ah, I see, the question needed editing to clarify the ranges. – RBarryYoung Sep 23 '13 at 09:50
  • @Ben No, technically this is not a Math problem, as Mathematics does not deal with compute time, only programming does that. – RBarryYoung Sep 23 '13 at 09:51
  • It's a geometric sum. They have a simple closed form solution. It takes about 30 nanoseconds to compute. – DanielV Sep 23 '13 at 09:51
  • @DanielV - false. Notice the modulo and the large `n`. – IVlad Sep 23 '13 at 09:52
  • Please Note the current question.I made a mistake just now. – Chenlin Zhang Sep 23 '13 at 09:54
  • 1
    I am assuming `a^n mod m` becomes periodic relatively fast for `m = k!, 1 <= k <= 12`. Have you tested this? – IVlad Sep 23 '13 at 09:56
  • @IVlad I don't test it,and you can do it and find some results. – Chenlin Zhang Sep 23 '13 at 09:57
  • I see. The point of having m=k! is so that it is unlikely that (a-1) is relatively prime to m, meaning that there will be no modular inverse to a-1. That actually makes this an interesting problem. – DanielV Sep 23 '13 at 10:12
  • @DanielV - yes, and the highest voted answer useless, sadly. – IVlad Sep 23 '13 at 10:21
  • @IVlad They only have to form cycles for p^n <= k, you can reassemble the answer with the chinese remainder theorem. – DanielV Sep 23 '13 at 10:25
  • nice problem, why closed? – Sayakiss Sep 23 '13 at 10:40
  • @IVlad: Wouldn't it be reasonable to use the same idea as here: http://stackoverflow.com/a/18891223/1009831? – Evgeny Kluev Sep 23 '13 at 10:51
  • 1
    @EvgenyKluev - not sure. In its original form where the entire thing was asked `mod m`, yes. In this form (notice that there's no `mod m` for the entire sum), it wouldn't really help. I'm thinking there needs to be a pattern, or `mod m` is missing at the end. I assume the fact that `m` is a factorial should also be used in the solution. – IVlad Sep 23 '13 at 10:57
  • @IVlad The link you give me is a simple version of this problem.It can't solve this problem.That problem using recursion to solve the problems – Chenlin Zhang Sep 23 '13 at 11:17

2 Answers2

5
1+a+a^2+...+a^n = (1+a+a^2+...+a^n)*(1-a)/(1-a) =
= (1 - a^(n+1))/(1-a)

In other words, your expression can be computed as:

(1 - a^(n+1))/(1-a) % m

Or, in programmatic form,

fmod((1-pow(a,n+1))/(1-a), m)
mvp
  • 111,019
  • 13
  • 122
  • 148
  • 1
    (+1) Awesome. Can you think of a reason `m` is restricted to being a factorial? – NPE Sep 23 '13 at 09:49
  • 1
    What if `1 - a` does not have a multiplicative inverse mod `m`? Using bignums will not be efficient for such a large `n`. This is nice, but doesn't solve the problem. – IVlad Sep 23 '13 at 09:50
  • Would there not be numerical issues around `a=1`? – NPE Sep 23 '13 at 09:52
  • Yes, it would. But it can be computed as separate case (trivial, I might add) – mvp Sep 23 '13 at 09:53
  • Sorry, by "around 1" I mean "near 1" rather than "at 1" (assuming `a` is a floating-point number). – NPE Sep 23 '13 at 09:54
  • pow(a,n+1) will exceed 64-bit integer.. if you use biginteger, it will get very slow.. – Sayakiss Sep 23 '13 at 09:57
  • @NPE: I think single formula will still compute it with higher precision compared to summing up many imprecise numbers – mvp Sep 23 '13 at 09:57
  • -1 until you address the no multiplicative inverse case and bignums being too slow issues. Also, the problem has slightly changed now. – IVlad Sep 23 '13 at 09:58
  • WTF! incredible that all of us are the same species.. – ZaoTaoBao Sep 23 '13 at 10:16
  • Try running fmod on a number with 10^18*log10(a) digits in it. It might take a bit longer than 3 minutes. – DanielV Sep 23 '13 at 10:29
0
 sum = 0;
 i = 0;
 while(i <= n){
 sum = sum + math.pow(a,i);
 i++;
 }
 result = sum % m;
user2315181
  • 29
  • 1
  • 4