I'm writing this but not getting output.
I'm new to solve project Euler problems in JavaScript
but getting errors please help me.
function pow(n, p) {
var r = (n, 0, 0);
var bn = (n, 0, 0);
for (var i = 1; i < p; i++) {
r = mult(r, bn);
}
return r;
}
function sumDigits(n) {
var bns = bigInt2str(n, 10);
var dStr = 0;
var a = bns.split('');
for (i in a) {
dStr += parseInt(a[i]);
}
return dStr;
}
console.log(sumDigits(pow(2, 1000)));
Please help me
You may use:
function pow(n, p) {
var r = bigInt(1); // (n, 0, 0);
var bn = bigInt(n); //var bn = (n, 0, 0);
for (var i = 0; i < p; i++) {
r = r.multiply(bn); //r = mult(r, bn);
}
return r;
}
function sumDigits(n) {
var bns = n.toString(); // bigInt2str(n, 10);
var dStr = 0;
var a = bns.split('');
for (i in a) {
dStr += parseInt(a[i]);
}
return dStr;
}
console.log(sumDigits(pow(2, 1000)));
output:
1366
ref:
https://www.npmjs.com/package/big-integer
installation If you are using a browser, you can download BigInteger.js from GitHub or just hotlink to it: <script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script> If you are using node, you can install BigInteger with npm. npm install big-integer Then you can include it in your code: var bigInt = require("big-integer");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.