challenge 2^i * 3^j * 5^k なる整数

2^i * 3^j * 5^k の形で表される整数を小さい方から順に 100 個列挙するプログラムを書いてください。 i, j, k は 0 以上の整数です。アルゴリズムのオーダーについても考えてみてください。

例えば最初の 10 個は次のようになります:

 1 = 2^0 * 3^0 * 5^0
 2 = 2^1 * 3^0 * 5^0
 3 = 2^0 * 3^1 * 5^0
 4 = 2^2 * 3^0 * 5^0
 5 = 2^0 * 3^0 * 5^1
 6 = 2^1 * 3^1 * 5^0
 8 = 2^3 * 3^0 * 5^0
 9 = 2^0 * 3^2 * 5^0
10 = 2^1 * 3^0 * 5^1
12 = 2^2 * 3^1 * 5^0

※解答では i, j, k の各値を示す必要はありません。

Posted feedbacks - HSP

関数名に困ったので、こういう数の集合を「アンダロート数」と呼ぶことにします。
 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
#module

#define true  1
#define false 0

// 可能な限り割り続ける
#defcfunc div_force int p1, int p2, var count, local val
    val   = p1
    count = 0
    
    repeat
        if ( (val \ p2) == 0 ) {
            val /= p2
            count ++
        } else {
            break
        }
    loop
    
    return val
    
// アンダロート数の集合かどうか
#defcfunc IsSetOfAndarote int p1, array condition, array result, local num, local count
    num = p1
    dim result, length(condition)
    foreach condition
        num = div_force(num, condition(cnt), result(cnt))
    loop
    return ( num == 1 )
    
#global

#define MAX_COUNT 100

*main
    dim condition, 3
        condition = 2, 3, 5
    dim result, 3
    sdim buf, 32000
    
    buf = "集合 { "
    foreach condition
        if ( cnt != 0 ) { buf += ", " }
        buf += condition(cnt)
    loop
    buf += " } に対するアンダロート数を"+ MAX_COUNT +"個列挙します。\n----------\n"
    
    count = 0
    repeat , 1
        if ( IsSetOfAndarote(cnt, condition, result) ) {
            buf += strf("%5d = ", cnt)
            foreach condition
                buf += "("+ condition(cnt) +" ^"+ strf("%2d", result(cnt)) +")"
            loop
            buf += "\n"
            count ++
            if ( count == MAX_COUNT ) { break }
        }
        await 0
    loop
    
    objmode 2
    mesbox buf, ginfo(12), ginfo(13)
    stop

Index

Feed

Other

Link

Pathtraq

loading...