Large powers in R -
i'm trying calculate 2**1000 , sum values of digits. intents , purposes, seems have right methodology wrong answer , i'm not sure if there's special way sums or maybe options messed up.
in case, 2**1000 in r gives: 1.072e+301. used
options("scipen"=400, "digits"=4) to rid of scientific notation (because want digits), gives me:
10715086071862673211222842640062602864002646240220400600628246062604626466468802860684246408802204448642628020644428680866666080884644840024840004280840848880462604626804200880464480884860464420284266864402822668802420668620402620400466086288824662642224206428624064400880244462828666486484022626226666 so naive thing , sum() of digits:
sum(1,0,7,1,5,0...) but answer out 1200, incorrect. can't think of how sum function returning funky, i'm assuming options screwed exponent result?
you can use gmp library work large integers:
library(gmp) bignum <- as.bigz(2)^1000 # want split characters can sum numbers: chars <- as.integer(strsplit(as.character(bignum), "")[[1]]) sum(chars) # 1366
Comments
Post a Comment