1 / 7

Some Programming Examples

Some Programming Examples. Complete number. In mathematics, a perfect number is defined as a positive integer which is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself E.g., 6=1+2+3. Complete number. for j in range(2,1001):

coen
Download Presentation

Some Programming Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Some Programming Examples

  2. Complete number In mathematics, a perfect number is defined as a positive integer which is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself E.g., 6=1+2+3

  3. Complete number for j in range(2,1001): k = [] n = -1 s = j for i in range(1,j): if j % i == 0: n += 1 s -= i k.append(i) if s == 0: print j

  4. Narcissistic number In number theory, a narcissistic number or pluperfect digital invariant (PPDI) or Armstrong number is a number that in a given base is the sum of its own digits to the power of the number of digits E.g., 153= 1**3+5**3+3**3

  5. Narcissistic number for n in range(100,1001): i = n / 100 j = n / 10 % 10 k = n % 10 if i * 100 + j * 10 + k == i + j ** 2 + k ** 3: print "%-5d" % n

  6. Monkey eats peaches A little monkey had some peaches. On the first day he decided to keep 1/2 of his peaches. He gave the rest away.Then he ate one. On the second day he decided to keep 1/2 of his peaches. He gave the rest away. Then he ate one. On the third day he decided to keep 1/2 of his peaches. He gave the rest away. Then he ate one. On the tenth day he found he had only one left. How many did he have at the beginning?

  7. Monkey eats peaches x2 = 1 for day in range(9,0,-1): x1 = (x2 + 1) * 2 x2 = x1 print x1

More Related