2020-09-30

Pandigital Squares

A pandigital number, like 4730825961, is a ten-digit number in which each digit from zero to nine appears exactly once. A square number, like 25² = 625, is a number with an integral square root. Your task is to write a program that finds all of the pandigital square numbers.

下面是嘗試的結果:

#!/usr/bin/env tclsh

set digital [list "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]

proc is_pandigital {n} {
    set length [string length $n]
    if {$length != 10 } {
        return 0
    }

    foreach mychar $::digital {
        if {[string first $mychar $n] == -1} {
            return 0
        }
    }

    return 1
}

set minvalue [expr int(floor(sqrt(1023456789)))]
set maxvalue [expr int(floor(sqrt(9876543210)))]

for {set n $minvalue} {$n <= $maxvalue} {incr n} {
    set num [expr $n * $n]
    if {[is_pandigital $num]==1} {
         puts $num
    }
}

2020-09-18

tksvg 0.4

tksvg 釋出新的版本 v0.4。

要注意的是,如果是使用 source code 自己編譯,需要更新 Makefile.in 才能夠正確安裝,下面就是 github 上的修改方式:


INSTALL        = @INSTALL@
-INSTALL_FLAGS   = @INSTALL_FLAGS@
-INSTALL_PROGRAM    = @INSTALL_PROGRAM@ $(INSTALL_FLAGS)
-INSTALL_LIBRARY    = @INSTALL_PROGRAM@ $(INSTALL_FLAGS)
+INSTALL_PROGRAM    = @INSTALL_PROGRAM@
+INSTALL_LIBRARY    = @INSTALL_PROGRAM@
INSTALL_DATA    = @INSTALL_DATA@
INSTALL_SCRIPT    = @INSTALL_SCRIPT@

2020-09-15

孫子問題

在《孫子算經》裡(共三卷,據推測約成書於西元400年左右), 下卷的第26題,就是鼎鼎有名的「孫子問題」:
今有物不知其數,三三數之剩二,五五數之剩三,七七數之剩二,問物幾何?

下面是使用迴圈的解法:
#!/usr/bin/env tclsh

proc find {n} {
    set i 0
    set a 0

    if {$n <= 0} {
        return 0;
    }

    while {$i < $n} {
         incr a
         if {[expr ($a%3)==2 && ($a%5)==3 && ($a%7)==2]==1} {
            incr i
         }
    }

    return $a
}

for {set num 1} {$num <= 10} {incr num} {
    puts "The $num answer is [find $num]."
}