Lucky numbers

Difference between version 0 and 0 - Previous - Next
[Arjen Markus] (16 april 2018) Lucky numbers are akin to prime numbers in that they are constructed via a sieve process too. They even have statistically similar properties. See [http://mathworld.wolfram.com/LuckyNumber.html%|%this page on Mathworld%|%] for instance. The process is simple enough:

   * Start with the odd numbers:
======
      1, 3, 5, 7, 9, 11, 13, 15, 17, 19, ...
======
   * Then, the second number in that list is 3, so remove every third number:
======
      1, 3, 7, 9, 13, 15, 19, ...
======
   * Then, the third number is 7, so remove every seventh number:
======
      1, 3, 7, 9, 13, 15, ...
======
   * And so on.

Thanks to the [[lmap]] command, the implementation in Tcl is also simple:

======
set numbers {}
for {set i 1} {$i < 1000} {incr i 2} {
    lappend numbers $i
}

#
# Loop over the remaining elements in the list:
# Filter out the nth element each time - a simple job for [lmap]
#
for {set k 1} {$k < [llength $numbers]} {incr k} {
    set step [lindex $numbers $k]

    set i 0
    set numbers [lmap v $numbers {
        incr i
        if {$i % $step} {
            set v
        } else {
            continue
        }
    }]
}

puts $numbers
======

<<categories>>Category Mathematics