简体   繁体   English

数的乘积,总和和位数

[英]Product, Sum, and Digits of a Number

My aim is to create a program that finds: 我的目的是创建一个查找以下内容的程序:

  • T(P(n)) : The sum of the product of the digits of all numbers up to n. T(P(n)) :所有不超过n的数字的乘积之和。 For example, T(p(5))= p(1)+p(2)+p(3)+p(4)+p(5), where p is a function that calculates the product of all nonzero digits in an integer. 例如,T(p(5))= p(1)+ p(2)+ p(3)+ p(4)+ p(5),其中p是一个函数,用于计算其中的所有非零数字的乘积一个整数。
  • T(S(n)) : Sum of all integers up to n, which is pretty easy using the n*(n+1)/2 formula T(S(n)) :直到n的所有整数的和,使用n *(n + 1)/ 2公式很容易
  • T(D(n)) : Sum of all the digits of all the integers up to n. T(D(n)) :直到n的所有整数的所有数字的总和。

I've attempted to complete the first 2 parts, as shown in the codepen link: https://codepen.io/anon/pen/LdjveQ . 我尝试完成前两部分,如codepen链接所示: https ://codepen.io/anon/pen/LdjveQ。 Javascript shown below: 如下所示的Javascript:

function result() {
var take = document.getElementById("number").value;
  return eval(take.replace(/(\d)(?=\d)/g, '$1+'));
}
function answer() {
document.getElementById('resultfinal').innerHTML = result();
}

function result2() {
var sake = document.getElementById("integers").value;
  var mult = (sake*(sake+1));
  return eval(mult/2);
}

function answer2() {
document.getElementById('sumofn').innerHTML = result2();
}

I have no idea: 我不知道:

  • T(P(n)) : How to approach the aspect of adding the p(n) of all integers greater than 0, but less than n. T(P(n)) :如何解决将大于0但小于n的所有整数的p(n)相加的问题。
  • T(D(n)) : Why the n(n+1)/2 formula produces the wrong result T(D(n)) :为什么n(n + 1)/ 2公式产生错误的结果
  • T(S(n)) : How I should approach T(D(n)) T(S(n)) :我应该如何接近T(D(n))

If a "brute force" approach is doable, I would first define P(n) as a function (se p function below) and sum it up using a for : 如果可以使用“蛮力”方法,我首先将P(n)定义为一个函数(下面的se p函数),然后使用for对其求和:

 function p(inputN) { var n = inputN, m = 1, digit; while (n) { digit = n % 10; if (digit) m *= digit; n = Math.floor(n / 10); } return m; } function result3() { var sum = 0, i, n = document.getElementById("tpn-input").value; for(var i = 1; i <= n; i++) { sum += p(i); } return sum; } function answer3() { document.getElementById('tpn').innerHTML = result3(); } 
 <input type="text" id="tpn-input"><button onClick="answer3()">P(n)</button> <p id = "tpn"></p> 

There are other somewhat more readable approaches using string .split() , .filter() together with .reduce() , but the above will certainly be more performatic. 存在使用串其他稍微更可读的方法.split() .filter()连同.reduce()但上述肯定会更performatic。


And, just for the heck of it, an over-oneliner short implementation: 而且,仅此而已,它是一个过于单一的简短实现:

 function p(n) { return n.toString().split('').map(Number).filter(i => i).reduce((a, b) => a * b, 1); } function tp(n) { return [...Array(+n).keys()].map(i => p(i+1)).reduce((a, b) => a + b, 0); } function answer3() { document.getElementById('tpn').innerHTML = tp(document.getElementById("tpn-input").value); } 
 <input type="text" id="tpn-input"><button onClick="answer3()">P(n)</button> <p id = "tpn"></p> 

You should start by trying using some of the array methods to iterate over an array from 1 to n . 您应该首先尝试使用一些数组方法来迭代从1n的数组。 reduce would be the most appropriate for this situation: it takes in an array and returns some cumulative value from an operation (or few) done over each element in the array. reduce将最适合这种情况:它接收一个数组,并从对该数组中的每个元素执行的一个操作(或几个操作)返回一些累加值。 For example: 例如:

 function sumUpToN(n) { return Array.from({ length: n + 1}, (_, i) => i) .reduce((partialSum, thisNum) => partialSum + thisNum); } console.log(sumUpToN(5)); 

Generate your arrays using Array.from like that, and then iterate over them with .reduce . 使用Array.from这样生成的数组,然后遍历他们.reduce The first argument is the returned value from the previous iteration of .reduce , and the second is the current element in the array. 第一个参数是.reduce上一次迭代的返回值,第二个参数是数组中的当前元素。 Try using a similar method when you need to iterate over the whole array and come out with a single value on the other end - except instead of plain addition (which will produce the sum of all the elements in the array), use whatever operator(s) are appropriate for the problem in question. 当您需要遍历整个数组并在另一端给出一个单一值时,请尝试使用类似的方法-除了代替普通加法(它将产生数组中所有元素的和)外,请使用任何运算符( s)适用于所讨论的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM