High power in R

I am trying to calculate 2 ** 1000 and sum the values ​​of digits. In all senses and purposes, it seems that I have the right methodology, but the wrong answer, and I'm not sure if there is a special way to make amounts, or maybe my options are spoiled.

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

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

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

10715086071862673211222842640062602864002646240220400600628246062604626466468802860684246408802204448642628020644428680866666080884644840024840004280840848880462604626804200880464480884860464420284266864402822668802420668620402620400466086288824662642224206428624064400880244462828666486484022626226666

So, I am doing a naive thing and doing the sum () of these numbers:

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

But the answer that I am leaving is 1200, which is incorrect. I can't figure out how the sum function will return something funky, so I assume that the parameters grafted the result of the exponent?

+1
source share
3 answers

gmp :

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
+1

2 ** 1000, BigInteger,

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

2 ** 1000 =

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

1366

+1

( , ):

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 extra package. Also tried in a vanilla session on 32 and 64-bit, and both of them give 1366.

0
source

All Articles