sum-of-digits

How to find sum of integers in a string using JavaScript

丶灬走出姿态 提交于 2020-02-26 04:25:47
问题 I created a function with a regular expression and then iterated over the array by adding the previous total to the next index in the array. My code isn't working. Is my logic off? Ignore the syntax function sumofArr(arr) { // here i create a function that has one argument called arr var total = 0; // I initialize a variable and set it equal to 0 var str = "12sf0as9d" // this is the string where I want to add only integers var patrn = \\D; // this is the regular expression that removes the

Sum of Digits using recursion in C

孤者浪人 提交于 2019-12-24 07:05:56
问题 For our activity today, we were tasked to make using recursion with the sum of digits. I already made this program: int main() { int num = 0, sum; printf("Enter an integer: "); scanf("%d",&num); //counter=1; for ( sum=0; num>0;) { sum = sum + num % 10; num = num /10; } printf("Sum = %d", sum); getch(); return 0; } Our teacher added "Input and output must be done in the main() function." Am doing the right thing? Or am I missing something in my code? 回答1: To do recursion, create a function

Sum of digits in C#

核能气质少年 提交于 2019-12-17 05:55:16
问题 What's the fastest and easiest to read implementation of calculating the sum of digits? I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21 回答1: You could do it arithmetically, without using a string: sum = 0; while (n != 0) { sum += n % 10; n /= 10; } 回答2: I use int result = 17463.ToString().Sum(c => c - '0'); It uses only 1 line of code. 回答3: For integer numbers, Greg Hewgill has most of the answer, but forgets to account for the n < 0. The sum of the digits of -1234 should still be 10,

Sum of digits in C#

走远了吗. 提交于 2019-12-17 05:54:06
问题 What's the fastest and easiest to read implementation of calculating the sum of digits? I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21 回答1: You could do it arithmetically, without using a string: sum = 0; while (n != 0) { sum += n % 10; n /= 10; } 回答2: I use int result = 17463.ToString().Sum(c => c - '0'); It uses only 1 line of code. 回答3: For integer numbers, Greg Hewgill has most of the answer, but forgets to account for the n < 0. The sum of the digits of -1234 should still be 10,

Sum of number using user entered 4 digit numbers

冷暖自知 提交于 2019-12-11 16:00:23
问题 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment_1 { class Program { static void Main(string[] args) { double SurfaceArea, Height, Radius, Volume; string inputString; int number; { //Title Text Console.WriteLine("Cylinder - Surface area and volume calculator"); //Prompts for input request Console.Write("Enter the radius: "); inputString = Console.ReadLine(); //displays user input for radius Console.WriteLine("{0}", inputString); Radius

Sum of factorials for large numbers

久未见 提交于 2019-12-05 03:38:03
问题 I want to calculate the sum of digits of N!. I want to do this for really large values of N, say N(1500). I am not using .NET 4.0. I cannot use the BigInteger class to solve this. Can this be solved by some other algorithm or procedure? Please help. I want to do some thing like this Calculate the factorial of an arbitrarily large number, showing all the digits but in C#. However I am unable to solve. 回答1: There is no special magic that allows you to calculate the sum of the digits, as far as

Sum of factorials for large numbers

牧云@^-^@ 提交于 2019-12-03 17:20:03
I want to calculate the sum of digits of N!. I want to do this for really large values of N, say N(1500). I am not using .NET 4.0. I cannot use the BigInteger class to solve this. Can this be solved by some other algorithm or procedure? Please help. I want to do some thing like this Calculate the factorial of an arbitrarily large number, showing all the digits but in C#. However I am unable to solve. There is no special magic that allows you to calculate the sum of the digits, as far as I am concerned. It shouldn't be that hard to create your own BigInteger class anyway - you only need to

Sum of digits of a factorial

为君一笑 提交于 2019-11-28 02:57:21
Link to the original problem It's not a homework question. I just thought that someone might know a real solution to this problem. I was on a programming contest back in 2004, and there was this problem: Given n, find sum of digits of n!. n can be from 0 to 10000. Time limit: 1 second. I think there was up to 100 numbers for each test set. My solution was pretty fast but not fast enough, so I just let it run for some time. It built an array of pre-calculated values which I could use in my code. It was a hack, but it worked. But there was a guy, who solved this problem with about 10 lines of

Sum of digits in C#

走远了吗. 提交于 2019-11-27 00:56:45
What's the fastest and easiest to read implementation of calculating the sum of digits? I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21 You could do it arithmetically, without using a string: sum = 0; while (n != 0) { sum += n % 10; n /= 10; } I use int result = 17463.ToString().Sum(c => c - '0'); It uses only 1 line of code. For integer numbers, Greg Hewgill has most of the answer, but forgets to account for the n < 0. The sum of the digits of -1234 should still be 10, not -10. n = Math.Abs(n); sum = 0; while (n != 0) { sum += n % 10; n /= 10; } It the number is a floating point number,

Sum of digits of a factorial

帅比萌擦擦* 提交于 2019-11-26 23:57:54
问题 Link to the original problem It's not a homework question. I just thought that someone might know a real solution to this problem. I was on a programming contest back in 2004, and there was this problem: Given n, find sum of digits of n!. n can be from 0 to 10000. Time limit: 1 second. I think there was up to 100 numbers for each test set. My solution was pretty fast but not fast enough, so I just let it run for some time. It built an array of pre-calculated values which I could use in my