Welcome, guest | Sign In | My Account | Store | Cart

elegant numbers are those numbers whose prime factors are 2,3,5,7 or 11.

Python, 99 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
w=1
counter=0
e=0


print"how many input"
x=input()


while w<=x:
    print'what number'
    y=raw_input()
    y=int(y)
    for i in range(2,100000000):
        q=0
        qq=0
        qqq=0
        qqqq=0
        qqqqq=0
        while i%2==0 and q%2==0:
            while q<>0 and q%2==0:
                q=q/2
            if q%2<>0:
                break
        
            if q==0:
                q=i/2
        while i%3==0 and qq%3==0:
            while qq<>0 and qq%3==0:
                qq=qq/3
            if qq%3<>0:
                break
            

            if q==0:
                qq=i/3
            if q<>0:
                qq=q/3
            
            
        while i%5==0 and qqq%5==0:
            while qqq<>0 and qqq%5==0:
                qqq=qqq/5
            if qqq%5<>0:
                break
            
            if q==0 and qq==0:
                qqq=i/5
            if q==0 and qq<>0:
                qqq=qq/5
            if q<>0 and qq==0:
                qqq=q/5
            if q<>0 and qq<>0:
                qqq=qq/5
            
                
                
        while i%7==0 and qqqq%7==0:
            while qqqq<>0 and qqqq%7==0:
                qqqq=qqqq/7
            if qqqq%7<>0:
                break
            
            
            if q==0 and qqq==0 and qq==0:
                qqqq=i/7
            if q<>0:
                qqqq=q/7
            if qq<>0:
                qqqq=qq/7
            if qqq<>0:
                qqqq=qqq/7
            
        while i%11==0:
            while qqqqq<>0 and qqqqq%11==0:
                qqqqq=qqqqq/11
            if qqqqq%11<>0:
                break
            

            if q==0 and qq==0 and qqq==0 and qqqq==0:
                qqqqq=i/11
            if q<>0:
                qqqqq=q/11
            if qq<>0:
                qqqqq=qq/11
            if qqq<>0:
                qqqqq=qqq/11
            if qqqq<>0:
                qqqqq=qqqq/11
                
        if qqqqq==1 or q==1 or qq==1 or qqq==1 or qqqq==1:
             counter=counter+1
        if counter==y:
            e=e+1
            print 'case',e,'the number is',i
            w=w+1
            counter=0
            break
                
    

3 comments

Matthew Wood 14 years, 1 month ago  # | flag

I hope this doesn't come off as rude, but what is your program supposed to do? I don't see any comments and the variable names (q, qq, qqq, qqqq, qqqqq, ...) aren't very enlightening.

I tried to run the program:

how many input
1
what number
3
case 1 the number is 4

And the program continued to run for several minutes, then exited.

Perhaps some I (or some other folks on this forum) can help document the code or potentially even refactor it to be more efficient.

mahthir (author) 14 years, 1 month ago  # | flag

well,matthew wood,this program gives the n-th elegant number.here,when it asks for how many input it actually means how many n-th number you want to know. then, when it asks what number, it actually asks what is the value of n.that is, if you enter 3,it will show you the 3rd elegant number.

Gabriel Genellina 14 years, 1 month ago  # | flag

Another variant using generators, heapq.merge and itertools.groupby:

from itertools import count, groupby
from heapq import merge

# generate all elegant numbers in order
elegant_withdupes = merge(
  (2*x for x in count(1)),
  (3*x for x in count(1)),
  (5*x for x in count(1)),
  (7*x for x in count(1)),
  (11*x for x in count(1)))
# remove duplicates
elegant = (k for k,v in groupby(elegant_withdupes))

# the first 20 elegant numbers:
for _ in range(20):
    print next(elegant),
# 2 3 4 5 6 7 8 9 10 11 12 14 15 16 18 20 21 22 24 25
Created by mahthir on Mon, 1 Mar 2010 (MIT)
Python recipes (4591)
mahthir's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks