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]."
}

2020-08-28

tDOM 0.9.2

tDOM 釋出了一個新的版本,0.9.2,可以在這裡找到檔案。

要注意的是,tDOM 似乎把 LICENSE 從 MPL 1.1 切換為 MPL 2.0。

2020-07-16

Binary Concatenation

嘗試用 Tcl 解題:
#!/usr/bin/env tclsh
#
# The concatenation of the first four integers, written in binary, is 11011100; 
# that is, 1 followed by 10 followed by 11 followed by 100. That concatenated 
# number resolves to 220. A similar process can convert the concatenation of 
# the first n binary numbers to a normal decimal number.
#

proc bitcat {number} {
    # Convert decimal to binary and collect the result
    set result ""
    for {set i 1} {$i <= $number} {incr i} {
        append result [format %llb $i]
    }

    return $result
}

puts -nonewline "Please input a number: "
flush stdout
gets stdin number
if {$number <= 0} {
    puts "Number requires > 0."
    exit
}

puts [expr (0b[bitcat $number]) % ((10 ** 9) + 7)]

也可以這樣解:
#!/usr/bin/env tclsh
#
# The concatenation of the first four integers, written in binary, is 11011100; 
# that is, 1 followed by 10 followed by 11 followed by 100. That concatenated 
# number resolves to 220. A similar process can convert the concatenation of 
# the first n binary numbers to a normal decimal number.
#
proc bitcat {result index} {
    for {set counter $index} {$counter > 0} {set counter [::tcl::mathop::>> $counter 1]} {
         set result [::tcl::mathop::<< $result 1]
    }

    return [::tcl::mathop::| $result $index]
}

proc solution {number} {
    set result 0
    for {set index 1} {$index <= $number} {incr index} {
        set result [bitcat $result $index]
    }

    return [expr $result % ((10 ** 9) + 7)]
}

puts -nonewline "Please input a number: "
flush stdout
gets stdin number
if {$number <= 0} {
    puts "Number requires > 0."
    exit
}
puts [solution $number]

2020-07-10

Trailing Zero-Bits

下面是我嘗試的答案:
#!/usr/bin/env tclsh
#
# Trailing Zero-Bits
# Given a positive integer, count the number of trailing zero-bits in its binary 
# representation. For instance, 18 = 10010, so it has 1 trailing zero-bit, 
# and 48 = 110000, so it has 4 trailing zero-bits.
#

puts -nonewline "Please input a number: "
flush stdout
gets stdin number
if {$number <= 0} {
    puts "Number requires > 0."
    exit
}

set trailing {{number} {
    set index 0
    while {[::tcl::mathop::& $number 1]!=1} {
        incr index
        set number [::tcl::mathop::>> $number 1]
    }
    return $index
}}

set count [apply $trailing $number]
puts $count

2020-07-04

tcljsonnet v0.13

首頁:
tcljsonnet


主要更新:
將 Jsonnet code base 版本升到 v0.16.0 版。

2020-06-29

Generate a file nv2808.xml

寫入資料到 nv-2808.xml。
Source code 搬到 github。


一個小工具。 XQCN 是 Qualcomm 使用的檔案格式,用來記錄 RF 相關的資料。其中 NV 2808 被用來設定為軟體版本的資訊(如果有使用的話)。

舉例來說,如果一個案子叫 FIREBIRD,地區是給北美 (NA) 使用的,目前的開發進度在 MP1,目前的版本是第一版,版本號就是 FIR_NA_MP1_001。這個工具就是用來產生 NV2808 的資料。

一般而言整合的方法有二個方式,第一個是將 NV-2808.xml 轉成 XQCN 檔案再使用 Qualcomm 提供的工具整合,第二個方式是將原始的 QCN 檔案轉為 NV XML 檔案,和 NV-2808.xml 整合以後再使用 Qualcomm 提供的工具轉為 XQCN 檔案。

總之,如果 XQCN 檔案也有放到版本管理中(例如 git),最終成品需要有版本號碼才行,用這樣的方式做 XQCN 檔案版本控管。

2020-06-06

Tcl: Hidden Squares

一個 string first 的範例,用來取得 Hidden Squares 問題的解:
#!/usr/bin/tclsh
#
# Hidden Squares:
# A number n may have squares hidden among its digits. For instance, 
# in the number 1625649, the consecutive digits 1, 4, 9, 16, 25, 49, 
# 64, 256 and 625 are all squares.
#

namespace path {::tcl::mathop ::tcl::mathfunc}

puts -nonewline "Please input a number: "
flush stdout
gets stdin number
if {$number <= 0} {
    puts "Number requires > 0."
    exit
}

set max [int [sqrt $number]]
for {set i 1} {$i <= $max} {incr i} {
    set n [* $i $i]
    if {[string first $n $number] >= 0} {
        puts -nonewline "$n "
    }
}
puts ""

這裡也使用了 namespace path。

Tcl 支援 namespace 的概念,一個 namespace 是 function 和變數的集合,和  C++ 類似,透過封裝可以保證不會被其它的 namespace 的變數和命令所影響,而且你可以隨時新增、改名與刪除裡面的成員。

2020-05-26

tcl-crc32c v0.1

tcl-crc32c: Tcl bindings for crc32c library.

只是一個練習,因為我太久沒用 C 寫 Tcl extension,剛好 Tcl 沒有這方面的實作,所以寫了一個 Tcl bindings。

PS.
crc32c 我有做一個 openSUSE RPM spec,用來產生 crc32c library 相關的檔案。

2020-05-10

Tcl: List Rotation

#!/usr/bin/tclsh
#
# Given a length-n list like (a b c d e), the rotations of the list are
# the n lists (a b c d e), (b c d e a), (c d e a b), (d e a b c), and (e a b c d), 
# in any order.
#
if {$argc == 0} {
    puts "Please input a string"
    exit
}

set len [llength $argv]
set rorateList $argv
for {set index 0} {$index < $len} {incr index} {
    puts $rorateList
    set first [lindex $rorateList 0]
    set rorateList2 [lrange $rorateList 1 [expr $len - 1]]
    lappend rorateList2 $first
    set rorateList $rorateList2
}

使用 lindex 取得頭以後,中間部份使用 lrange 取得,然後再排列出來。

2020-05-09

tcl.mqttc v0.7

我發現 paho.mqtt.c 有一個新的版本 1.3.2,所以我做了 tcl.mqttc 相關的更新,同時將版本設成 v0.7。

不過我只有簡單的測試 tcp 部份,使用 ActiveMQ 測試 MQTT 3.1 未加密的傳送與接收。