A computer science problem by Paola Ramírez

How many positive four digits numbers exist such that the sum of their digits is greater than 33?


The answer is 15.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

5 solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int sum(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n -= (n % 10);
    n /= 10;
  }
  return s;
}
main()
{
  int i = 1000, s = 0;
  while(i++ < 10000)  if(sum(i) > 33) s++;
  cout << s;
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def sum(n):
  s = 0
  while n:
    s += n % 10
    n -= (n % 10)
    n /= 10
  return s
i = 1000
s = 0
while i < 10000:
  if sum(i) > 33:
    s += 1
  i += 1
print(s)

Paola Ramírez
May 20, 2016

This problem I solved with excel but also could be solved using combinatorics

How did you do it with Excel?

Adhiraj Dutta - 1 year, 5 months ago
Hasmik Garyaka
Sep 17, 2017

print (len ([i for i in range(1000,10000) if sum(map(int, str(i)))>33]))

Pablo Ruiz
Oct 13, 2016

A solution Carlos García and Pablo coded in Java

public class ProblemaPaolaBrilliant{

 public static void main(String []args){
    int contador=0;
    int suma;
    for (int i=1000;i<10000;i++){
        String x=""+i;
        String p=x.substring(0,1);
        String s=x.substring(1,2);
        String t=x.substring(2,3);
        String c=x.substring(3,4);
        suma= Integer.parseInt(p) + Integer.parseInt(s) + Integer.parseInt(t) + Integer.parseInt(c);
        if (suma>33){
            contador++;
        }

    }
    System.out.println(contador);

 }

}

1
2
3
4
5
6
7
8
9
def digit_sum(num):
  return sum(map(int, str(num)))

ctr = 0
for n in range(1000, 10000):
  if digit_sum(n) > 33:
    ctr += 1

print ctr

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...