AI52487963
AI52487963

Reputation: 1273

Large powers in R

I'm trying to calculate 2**1000 and sum up the values of the digits. For all intents and purposes, it seems like I have the right methodology but the wrong answer and I'm not sure if there's a special way to do sums or maybe my options are messed up.

In any case, 2**1000 in R gives: 1.072e+301. I used

options("scipen"=400, "digits"=4)

to get rid of the scientific notation (because we want the digits), which gives me:

10715086071862673211222842640062602864002646240220400600628246062604626466468802860684246408802204448642628020644428680866666080884644840024840004280840848880462604626804200880464480884860464420284266864402822668802420668620402620400466086288824662642224206428624064400880244462828666486484022626226666

So I do the naive thing and do a sum() of those digits:

sum(1,0,7,1,5,0...)

But the answer I get out is 1200, which is incorrect. I can't think of how the sum function would be returning something funky, so I'm assuming the options screwed up the exponent result?

Upvotes: 1

Views: 1341

Answers (3)

plannapus
plannapus

Reputation: 18749

Or here is another way to get the correct answer (the trick is to treat the large integer as a character string to do the splitting of the digits):

options("scipen"=400, "digits"=4)
a <- 2^1000
a
[1] 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
b <- strsplit(as.character(a),"")[[1]]
b
  [1] "1" "0" "7" "1" "5" "0" "8" "6" "0" "7" "1" "8" "6" "2" "6" "7" "3" "2" "0" "9" "4" "8" "4" "2" "5" "0" "4" "9" "0" "6" "0" "0" "0" "1" "8" "1" "0" "5" "6" "1"
 [41] "4" "0" "4" "8" "1" "1" "7" "0" "5" "5" "3" "3" "6" "0" "7" "4" "4" "3" "7" "5" "0" "3" "8" "8" "3" "7" "0" "3" "5" "1" "0" "5" "1" "1" "2" "4" "9" "3" "6" "1"
 [81] "2" "2" "4" "9" "3" "1" "9" "8" "3" "7" "8" "8" "1" "5" "6" "9" "5" "8" "5" "8" "1" "2" "7" "5" "9" "4" "6" "7" "2" "9" "1" "7" "5" "5" "3" "1" "4" "6" "8" "2"
[121] "5" "1" "8" "7" "1" "4" "5" "2" "8" "5" "6" "9" "2" "3" "1" "4" "0" "4" "3" "5" "9" "8" "4" "5" "7" "7" "5" "7" "4" "6" "9" "8" "5" "7" "4" "8" "0" "3" "9" "3"
[161] "4" "5" "6" "7" "7" "7" "4" "8" "2" "4" "2" "3" "0" "9" "8" "5" "4" "2" "1" "0" "7" "4" "6" "0" "5" "0" "6" "2" "3" "7" "1" "1" "4" "1" "8" "7" "7" "9" "5" "4"
[201] "1" "8" "2" "1" "5" "3" "0" "4" "6" "4" "7" "4" "9" "8" "3" "5" "8" "1" "9" "4" "1" "2" "6" "7" "3" "9" "8" "7" "6" "7" "5" "5" "9" "1" "6" "5" "5" "4" "3" "9"
[241] "4" "6" "0" "7" "7" "0" "6" "2" "9" "1" "4" "5" "7" "1" "1" "9" "6" "4" "7" "7" "6" "8" "6" "5" "4" "2" "1" "6" "7" "6" "6" "0" "4" "2" "9" "8" "3" "1" "6" "5"
[281] "2" "6" "2" "4" "3" "8" "6" "8" "3" "7" "2" "0" "5" "6" "6" "8" "0" "6" "9" "3" "7" "6"
sum(as.integer(b))
[1] 1366

This works with my settings (i. e. Mac OS X with R 2.14.2, 32-bit) without any additional package. Also tried in a vanilla session on 32 and 64-bit and they both give 1366.

Upvotes: 0

Scott Ritchie
Scott Ritchie

Reputation: 10543

You can use the gmp library to work with large integers:

library(gmp)
bigNum <- as.bigz(2)^1000
# Now we want to split into characters so we can sum the numbers:
chars <- as.integer(strsplit(as.character(bigNum), "")[[1]])
sum(chars)
# 1366

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

In order to compute 2**1000 you have to use BigInteger equivalent, e.g.

http://cran.at.r-project.org/web/packages/Brobdingnag/index.html

The right answer is

2**1000 =

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

The sum of digits is 1366

Upvotes: 1

Related Questions