Questions tagged [sum-of-digits]

A sum of digits, or digital sum, is a number created by taking each digit of one or more other numbers and adding them together.

For example, the sum of digits for the number 12,345 would be 15 because:

1 + 2 + 3 + 4 + 5 = 15

The sum of digits for the numbers 12,345 and 67,890 together would be 45 because:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 = 45

Note that a sum of digits will always be an integer.

60 questions
51
votes
10 answers

Sum of digits of a factorial

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…
Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
47
votes
18 answers

Sum of digits in C#

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
Xn0vv3r
  • 17,766
  • 13
  • 58
  • 65
9
votes
4 answers

Certain Power of Sum of Digits of N == N (running too slowly)

I'm trying to write a Python script that finds all integers (N) where a certain power of the sum of the digits of N is equal to N. For example, N=81 qualifies, because 8 + 1 = 9, and a certain power of 9 (namely 2) = 81. The ranges I chose are…
5u2ie
  • 93
  • 8
3
votes
7 answers

Sum of factorials for large numbers

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…
user873244
  • 51
  • 1
  • 4
3
votes
1 answer

get sum of bigInt number golang

Hi i am new to the golang programming language. I can get the bigint value from the factoral function but it is not working with the add function. i have had the add function accepting bigint but when i try to add a .Mod and .Div methods it…
user2963022
  • 165
  • 3
  • 16
3
votes
3 answers

sum quantity of dictionaries in array

I have a NSArray containing a list of NSDictionary. Like: NSArray *array = ...; NSDictionary *item = [array objectAtIndex:0]; NSLog (@"quantity: %@", [item objectForKey: @"quantity"]); How can I sum all the quantities contained in all dictionaries…
lolol
  • 4,287
  • 3
  • 35
  • 54
2
votes
0 answers

Digit Dynamic Programming Problem For Sum of Numbers

I want to find the sum of all the positive integers in the range [1, N] with a given digit sum d. For example, if n = 100 and d = 7, the answer will be 7 + 16 + 25 + 34 + 43 + 52 + 61 + 70 = 308. Following code can be used to count the numbers in…
user2784016
  • 179
  • 1
  • 7
2
votes
1 answer

How to optimize and find output for large inputs?

For an input number N, I am trying to find the count of numbers of special pairs (x,y) such that the following conditions hold: x != y 1 <= N <= 10^50 0 <= x <= N 0 <= y <= N F(x) + F(y) is prime where F is sum of all digits of the number finally…
Ronak Jain
  • 21
  • 3
2
votes
2 answers

Python program find sum of consecutive number values equal to number n?

I want to find consecutive digits in a string that sum to a given number. Example: a="23410212" number is=5 — output 23,41,410,0212,212. This code is not working. What do I need to fix? def find_ten_sstrsum(): num1="2825302" n=0; …
Karthick Anbazhagan
  • 353
  • 2
  • 9
  • 27
2
votes
3 answers

Positive integers in array sum to a negative number

I have this piece of code: int[] primes = generatePrimes(bound); int sum = 0; for (int i = 0; i < primes.GetLength(0); i++) { if (sum < 0) { Console.WriteLine(sum); } sum += primes[i]; } I have checked to make sure that my…
1
vote
2 answers

Find number of pairs that add up to a specific number from two different lists?

a = [1,2,3,4,5,6,7] b = [56,59,62,65,67,69] def sumOfTwo(a,b,v): for i in range (len(a)): val_needed = v - a[i] for j in range (len(b)): if b[j] == val_needed: x = b[j] y = a[i] …
Avinash Singh
  • 122
  • 2
  • 9
1
vote
2 answers

How To Split An Integer

I am trying to create an additive persistence program where the user inputs a number and the program outputs the total number of iterations (or additive persistence). I want to code it so that I do not use the string data type. Are there any…
FZCube42
  • 35
  • 1
  • 8
1
vote
3 answers

Sum of digits with test cases in C

I am trying to find the sum of digits with test cases. But the problem is after I find one sum, this sum is adding to the next sum but I only one particular sum of that numbers' digit. Please help. Here is my code: #include int main() { …
1
vote
3 answers

Algorithm to sum up all digits of a number

Can you please explain to me how this loop works? What is going on after first loop and after second etc. def sum(n): s = 0 while n: s += n % 10 n /= 10 return s >>> print sum(123) 6
sakmario
  • 43
  • 4
1
vote
0 answers

How to display SUM of total in email HTML page with SQL only?

I am generating report to display in Outlook with SQL Server ONLY.(no editor used) Here is my codes. I have column name "Fee" in my DB. I need to sum the total fee and display in HTML table as "Total Fee". As I have already using AS for [Fee] AS…
shire
  • 11
  • 1
1
2 3 4