Guest User

Math problemo

a guest
May 5th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. #Determine the number of positive integers x, where x is
  2. #less than equal to 9,999,999 and the sum of the digits in x is 31.
  3. a=0
  4. b=0
  5. c=0
  6. d=0
  7. e=0
  8. f=0
  9. g=0
  10. solution=0
  11.  
  12. while 1000000*a+100000*b+10000*c+1000*d+100*e+10*f+g<=9999999:
  13.     if a+b+c+d+e+f+g==31:
  14.         solution+=1
  15.     if g<9:
  16.        g+=1
  17.     else:
  18.         if f<9:
  19.             f+=1
  20.         else:
  21.             f=0
  22.             if e<9:
  23.                 e+=1
  24.             else:
  25.                 e=0
  26.                 if d<9:
  27.                     d+=1
  28.                 else:
  29.                     d=0
  30.                     if c<9:
  31.                         c+=1
  32.                     else:
  33.                         c=0
  34.                         if b<9:
  35.                             b+=1
  36.                         else:
  37.                             b=0
  38.                             if a<9:
  39.                                 a+=1
  40. print(solution)
Advertisement
Add Comment
Please, Sign In to add comment