Version 36 of Collatz_Sequences (3*N+1) in Console Example Demo for TCL V2

Updated 2021-09-12 18:32:21 by gold

Collatz_Sequences (3*N+1) in Console Example Demo for TCL V2

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 20Aug2020


Preface


Introduction


Here are TCL calculations on the Collatz Sequences. The Collatz Sequences were named after German mathematician Lothar Collatz (1910–1990). Collatz conjectured that the Collatz Sequence of any positive number would begin repeating and coalesce to 1. The proc collatz_sequence was substantially rewritten by HE and replaced in the main deck and comments, which I will have to chew on. Primarily, I am interested in petitioning the code to the TCLLIB. Many thanks to HE for cimments and feedback.


The collatz_sequence proc returns the partial collatz_sequence from positive integers. This collatz_sequence proc can easily generate an endless loop. In fact, the known collatz_sequences are an endless list or repetitive string of integers. The iteration number is a stop or limit to iteration. By custom, the end of the partial collatz_sequence is 1 or start of repetition. For example, the collatz_sequence for integer 5 would be < 5 16 8 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 ...> The proc with entry 5 is intended to report the partial collatz_sequence of < 5 16 8 4 2 1 >. Many of the known collatz_sequences boil down to end in < ... 16 8 4 2 1 ... > , so its somewhat counter intuitive to me that various positive integers have the same terminus in their collatz_sequence.


if $N even, return { $N / 2 }. If $N odd, return (3*$N+1). Continue the collatz_sequence until returned item is 1 or start of repetition. The speed of the supporting procs IsOdd and IsEeven were undetermined.



Comments and Rewrite by HE


HE 2021-09-11: Hello Gold, I have a bunch of suggestions/questions about the code. Mainly in respect to other operating systems but, also if one is using your code in a win10 or related system. This is not to bother you.


As far as I understand your code is planned to be executed on a command line. There is no input. And, output is completely done by using 'puts'. It also use only commands/procedures provided by a Tcl only installation (beside some lines I will discuss below). That means the code should be possible to execute everywhere where we can call a tclsh and copy or source the code into it.


Poorly this doesn't work. Because the code contains "package require Tk" without using any GUI and some installation will not have a Tk. And using the command 'console' which is only available in wish.exe but not in wish compiled for Linux, for example.


For sure some could simply copy the code between "package require Tk" and the usage of 'console' at the end to try to get it running. That will not work because there is a 'console show' in between. This needs also to be excluded. At the end the user interested of the result of your script needs to investigate the code to get it running. A bit inconvenience from my point of view. Particularly because all the problematic lines are mainly for eye candy only.


How would I try to execute this type of scripts: On win10 I would open a wish. It displays a console window. I would make a copy and past (c&p) of the code into this console window. The output would be crude to read because of:

  • all the output before "proc table_format_out ..." would not be easy to read because the 'puts' lines and the output are interchanging.
  • The change of font and geometry left a console window far to big on some displays and not able to show the text in a readable way. At least not for me.
  • I don't discuss the format of the able output here, because this also not help to read the results.

Next try would be to open a tclsh on win10 and c&p the code into it. The result is similar. The font size and console background can't be changed so it is more comfortable to read. But the other issues mentioned above still exists and in addition we have the error message in between about the 'console' command.


Next try would be to store it in a file, start a tclsh on win10 (because "package require Tk" let me think it is planned to do so) and source the file into it. This simply raise a "Invalid command name "console"". This will be the same on Linux.


Next try would be to start a wish on win10 and source the stored file into it. I think that is the result you wanted. But, eye candy for you on your computer doesn't fit well on my computer. You also don't know if the user will have problems with a green background. And a smaller font size would show more from the table. At least on the computer I tried your code.


Now I tried all four variants on Linux. Sourcing the file lead in both cases to error message "invalid command name "console"" without any other output. C&p into a wish or tclsh lead to the ugly output where it is not clear where to look for the wanted result.


What I would change:

  • To be platform independent I would remove "package require Tk" and the 'console' lines.
  • To be able to run it by double-click inside win10 I would add somewhere at the top of the script:

if {$tcl_platform(platform) == {windows}} {
        console show
}

  • I would put all output into one procedure so that the last command could this procedure and afterwards all output is generated.

Sorry again, for all the comments only because of output and executing. I tend to write to much in case I try to explain my view. But look how easy it is to make the code run in 8 different conditions instead only running under one condition.


Until here, I read so often this piece of code that I found some small other items:

  • Procedures isPositive and isNegative are not used. Perhaps, isPositive was planned to be used in collatz_sequence as a protection not to use negative numbers?
  • "set tclprecision 17" is used. The Tcl 8.6 documentation tells "It defaults to 0. Applications should not change this value;". I'm not sure to understand why it is used. The whole math of that problem should be integer calculation. At least until the limit of 8 byte integer is crossed. And as long as no fraction is forced the math should stay integer. Perhaps, I'm wrong. But, that is I understood how Tcl calculation works.
  • Where you are using the math packages you load in the beginning? I can't find the usage of it. I would understand "package require math::bignum" to be able to calculate behind the 8 byte integer limit. But the named one?

HE 2021-09-12: Since my comment yesterday, all comment lines are changed from '#' to ';#' at the beginning. Is there a reason for this? From my knowledge a ';' is not need for a proper comment at the beginning of a line. Only if the comment is behind a command on the same line a ';' is needed before the '#' to separate both commands.


HE 2021-09-12: As an answer of your question on Ask, and it shall be given # 13 here my version of collatz_sequence based on your one.

  • For loop provide everything to control iteration.
  • Some tests in the beginning to assure that positive integers are used for number and iteration.

That makes it easier to stop as we find 1 as a result. At least the first 10 million integer I tried to use for number will all end with 1 and then go to the endless sequence. As far as I understood others are tried far higher numbers with the same end. Also I would not check separately for odd and even. One check is enough. And I used the expression itself instead of put it into a separate procedure. This will spare calculation power with bigger numbers.


proc collatz_sequence {number iteration} {
        # Check parameter
        if {!([string is wideinteger $number] && $number > 0)} {
                error "Number '$number' is not positiv integer!"
        }
        if {!([string is wideinteger $iteration] && $iteration > 0)} {
                error "Iteration '$iteration' is not positiv integer!"
        }
        # Initialisation
        set sequence [list $number]

        # Interations
        for {set n 0} {$n <= $iteration} {incr n} {
                if {$number == 1} {
                        return $sequence
                }
                if {$number % 2} {
                        # Odd
                        set number [expr {(3 * $number) + 1}]
                } else {
                        set number [expr {$number / 2}]
                }
                lappend sequence $number
        }
        # After value reach 1 it is a endless sequence of 4 2 1 4 2 1 ...
        # If we reach not 1 during the given ammount of iteration
        # we raise an error to signal that number of iterations are to small
        # or we reach the case all looking for.
        error "Number of iteration '$iteration' reached without getting 1!"
}

Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 12Aug2020

HE 2021-09-11: Hello Gold, I have a bunch of suggestions/questions about the code. Mainly in respect to other operating systems but, also if one is using your code in a win10 or related system. This is not to bother you.

As far as I understand your code is planned to be executed on a command line. There is no input. And, output is completely done by using 'puts'. It also use only commands/procedures provided by a Tcl only installation (beside some lines I will discuss below). That means the code should be possible to execute everywhere where we can call a tclsh and copy or source the code into it.

Poorly this doesn't work. Because the code contains "package require Tk" without using any GUI and some installation will not have a Tk. And using the command 'console' which is only available in wish.exe but not in wish compiled for Linux, for example.

For sure some could simply copy the code between "package require Tk" and the usage of 'console' at the end to try to get it running. That will not work because there is a 'console show' in between. This needs also to be excluded. At the end the user interested of the result of your script needs to investigate the code to get it running. A bit inconvenience from my point of view. Particularly because all the problematic lines are mainly for eye candy only.

How would I try to execute this type of scripts: On win10 I would open a wish. It displays a console window. I would make a copy and past (c&p) of the code into this console window. The output would be crude to read because of:

  • all the output before "proc table_format_out ..." would not be easy to read because the 'puts' lines and the output are interchanging.
  • The change of font and geometry left a console window far to big on some displays and not able to show the text in a readable way. At least not for me.
  • I don't discuss the format of the able output here, because this also not help to read the results.

Next try would be to open a tclsh on win10 and c&p the code into it. The result is similar. The font size and console background can't be changed so it is more comfortable to read. But the other issues mentioned above still exists and in addition we have the error message in between about the 'console' command.

Next try would be to store it in a file, start a tclsh on win10 (because "package require Tk" let me think it is planned to do so) and source the file into it. This simply raise a "Invalid command name "console"". This will be the same on Linux.

Next try would be to start a wish on win10 and source the stored file into it. I think that is the result you wanted. But, eye candy for you on your computer doesn't fit well on my computer. You also don't know if the user will have problems with a green background. And a smaller font size would show more from the table. At least on the computer I tried your code.

Now I tried all four variants on Linux. Sourcing the file lead in both cases to error message "invalid command name "console"" without any other output. C&p into a wish or tclsh lead to the ugly output where it is not clear where to look for the wanted result.

What I would change:

  • To be platform independent I would remove "package require Tk" and the 'console' lines.
  • To be able to run it by double-click inside win10 I would add somewhere at the top of the script:
if {$tcl_platform(platform) == {windows}} {
        console show
}
  • I would put all output into one procedure so that the last command could this procedure and afterwards all output is generated.

Sorry again, for all the comments only because of output and executing. I tend to write to much in case I try to explain my view. But look how easy it is to make the code run in 8 different conditions instead only running under one condition.

Until here, I read so often this piece of code that I found some small other items:

  • Procedures isPositive and isNegative are not used. Perhaps, isPositive was planned to be used in collatz_sequence as a protection not to use negative numbers?
  • "set tclprecision 17" is used. The Tcl 8.6 documentation tells "It defaults to 0. Applications should not change this value;". I'm not sure to understand why it is used. The whole math of that problem should be integer calculation. At least until the limit of 8 byte integer is crossed. And as long as no fraction is forced the math should stay integer. Perhaps, I'm wrong. But, that is I understood how Tcl calculation works.
  • Where you are using the math packages you load in the beginning? I can't find the usage of it. I would understand "package require math::bignum" to be able to calculate behind the 8 byte integer limit. But the named one?

HE 2021-09-12: Since my comment yesterday, all comment lines are changed from '#' to ';#' at the beginning. Is there a reason for this? From my knowledge a ';' is not need for a proper comment at the beginning of a line. Only if the comment is behind a command on the same line a ';' is needed before the '#' to separate both commands.

HE 2021-09-12: As an answer of your question on Ask, and it shall be given # 13 here my version of collatz_sequence based on your one. For loop provide everything to control iteration. Some tests in the beginning to assure that positive integers are used for number and iteration. That makes it easier to stop as we find 1 as a result. At least the first 10 million integer I tried to use for number will all end with 1 and then go to the endless sequence. As far as I understood others are tried far higher numbers with the same end. Also I would not check separately for odd and even. One check is enough. And I used the expression itself instead of put it into a separate procedure. This will spare calculation power with bigger numbers.

proc collatz_sequence {number iteration} {
        # Check parameter
        if {!([string is wideinteger $number] && $number > 0)} {
                error "Number '$number' is not positiv integer!"
        }
        if {!([string is wideinteger $iteration] && $iteration > 0)} {
                error "Iteration '$iteration' is not positiv integer!"
        }
        # Initialisation
        set sequence [list $number]

        # Interations
        for {set n 0} {$n <= $iteration} {incr n} {
                if {$number == 1} {
                        return $sequence
                }
                if {$number % 2} {
                        # Odd
                        set number [expr {(3 * $number) + 1}]
                } else {
                        set number [expr {$number / 2}]
                }
                lappend sequence $number
        }
        # After value reach 1 it is a endless sequence of 4 2 1 4 2 1 ...
        # If we reach not 1 during the given ammount of iteration
        # we raise an error to signal that number of iterations are to small
        # or we reach the case all looking for.
        error "Number of iteration '$iteration' reached without getting 1!"
        }

--- gold Many thanks to HE for comments and feedback. In terms of use on other TCL platforms, I am interested in petitioning the code to the TCLLIB. On my own Windows10, mostly I attach a proven subroutine to the command line interface and type into the TCL console, meaning Easy Eye Calculator and eTCL Slot Calculator Demo Example, Numerical Analysis. At present, all i do is type on the icon of the Easy Eye console and I am up and running. I am not sure how one could test a numeric program without some listing of canned test cases, prinout, or wiki style printout? I could try to reorganize the script and put the canned testcases and cosmetics to the end of script.

---

References:


  • Wikipedia search engine < Collatz Sequences >
  • Wikipedia search engine < Lothar Collatz Sequences >
  • Wikipedia search engine < Programming Examples >
  • Google search engine < vaporware >
  • Google search engine < Collatz_Sequences >
  • Book >> How to Prove The Collatz Conjecture Paperback, 2005, by Danny Fleming
  • Ultimate Challenge: the 3x + 1 problem: edited by Jeffrey Lagarias,
  • One Liners Programs Pie in the Sky
  • One Liners
  • One Liners Programs Compendium [L1 ]
  • WIKI BOOKS, Programming_Examples pdf
  • WIKI BOOKS, Tcl_Programming_Introduction pdf
  • google search engine < Collatz Conjecture >
  • switch
  • Tcl Tutorial Lesson 6
  • Math Sugar Richard Suchenwirth RS
  • Practical_Advice_on_Quotes_and_Brackets_in_TCL
  • tmml.sourceforge.net/doc/tcllib [L2 ]
  • rosettacode.org/wiki/Even_or_odd [L3 ]
  • en.wikipedia.org/wiki/Collatz_conjecture
  • Tao, Terence , 2019. "Almost all Collatz orbits attain almost bounded values".
  • Project Euler 14: Longest Collatz sequence, Peter Prevos
  • Mathematician Proves Huge Result on ‘Dangerous’ refer to Terence Tao
  • Terence Tao realized that the Collatz conjecture was similar to partial differential equations.
  • The Tao starting sample weighs toward numbers that
  • have a remainder of 1 after being divided by 3,
  • and away from numbers that have a remainder of 2 after being divided by 3.
  • TCL pseudocode
  • remove all numbers divisible by 3 on a list of integers,
  • find all numbers that have a remainder of 1, when divided by 3.


Screenshots Section


figure 1. Screenshot, Collatz_Sequences_(3*N+1) _screenshot


Collatz_Sequences_(3*N+1) _screenshot



Testcases Section

In planning any software, it is advisable to gather a number of testcases to check the results of the program.

Testcase 1, Partial Collatz_Sequences



table, Partial Collatz_Sequences printed inTCL format
number iteration partial Collatz_Sequences comment, if any
3 5 3 10 5 16 8 4 2 1
4 5 4 2 1
5 5 5 16 8 4 2 1
6 5 6 3 10 5 16 8 4 2
7 5 7 22 11 34 17 52 26 13 40
8 5 8 4 2 1
9 5 9 28 14 7 22 11 34 17 52
10 5 10 5 16 8 4 2 1
11 5 11 34 17 52 26 13 40 20
12 5 12 6 3 10 5 16 8 4
13 5 13 40 20 10 5 16 8
14 5 14 7 22 11 34 17 52 26 13 40
15 5 15 46 23 70 35 106 53 160 80
16 5 16 8 4 2 1
17 5 17 52 26 13 40 20 10
18 5 18 9 28 14 7 22 11 34 17 52
19 5 19 58 29 88 44 22 11 34
20 5 20 10 5 16 8 4 2


Appendix TCL programs and scripts

* Pretty Print Version


            ;# pretty print from autoindent and ased editor
            ;# Collatz_Sequences (3*N+1) in Console Example Demo for TCL V2
            ;# Console program example demo
            ;# written on Windows 10 on  TCL
            ;# working under TCL version 8.6
            ;# gold on TCL Club , 11sep2021
            package require Tk
            package require math::numtheory
            package require math::constants
            package require math::trig
            package require math
            namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory math::trig math::constants }
            set tclprecision 17
            ;# logic tests for even,  odd, positive, negative,  conditions of positive  numbers
            ;# uses logic inside expr , if positive return 1, if not positive return 0
            proc isPositive x {return [ expr {$x>0}]}
            ;# Richard Suchenwirth RS idea  in Math Sugar
            ;# uses logic inside expr, if negative return 1, if not negative return 0
            proc isNegative  x { return [ expr {$x<0}]}
            ;# Richard Suchenwirth RS idea from wiki: Math Sugar
            ;# idea by Michael Barth
            ;# No error traps or safety nets here for negative and real numbers.
            ;# conditional proc  IsOdd,  if N odd, return 1, if N not odd, return 0.
            proc IsOdd { N }  { return [ expr {$N % 2} ] }
            ;# Usage
            IsOdd 5  ;# returns 1
            ;# conditional proc IsEven,  if N even,  return 1. If N not even return 0.
            proc IsEven { N }  { return [ expr { ( $N + 1 ) % 2} ] }
            IsEven 4 ;# returns 1
            ;#
            ;# Results: collatz_sequence
            ;#
            ;# Return the collatz_sequence from positive integer
            ;#
            ;# Arguments:
            ;#    number first value is number
            ;#    iteration second value is iteration
            ;#
            ;#
            ;# Note: This program can easily generate an endless loop.
            ;#        iteration number is a stop or limit to iteration
            ;# By custom, the end of collatz_sequence is 1 or start of repetition
            proc collatz_sequence {number iteration} {
                ;# Rewritten by HE on TCL WIKI
                ;# Check parameter
                if {!([string is wideinteger $number] && $number > 0)} {
                    error "Number '$number' is not positive integer!"
                }
                if {!([string is wideinteger $iteration] && $iteration > 0)} {
                    error "Iteration '$iteration' is not positiv integer!"
                }
                ;# Initialisation
                set sequence [list $number]
                
                ;# Interations
                for {set n 0} {$n <= $iteration} {incr n} {
                    if {$number == 1} {
                        return $sequence
                    }
                    if {$number % 2} {
                        ;# Odd
                        set number [expr {(3 * $number) + 1}]
                    } else {
                        set number [expr {$number / 2}]
                    }
                    lappend sequence $number
                }
                ;# After value reach 1 it is a endless sequence of 4 2 1 4 2 1 ...
                ;# If we reach not 1 during the given ammount of iteration
                ;# we raise an error to signal that number of iterations are to small
                ;# or we reach the case all looking for.
                ;# this error crops with large lists > 15 elements
                ;# error "Number of iteration '$iteration' reached without getting 1!"
            }
            console show
            puts "  Console wrapper for solution proc"
            puts "  ***************************"
            puts "  ***************************"
            puts "collatz_sequence 31 130 collatz_sequence [collatz_sequence 31 130]"
            puts "collatz_sequence 11 15 collatz_sequence [collatz_sequence 11 15]"
            puts "collatz_sequence 10 15 collatz_sequence [collatz_sequence 10 15]"
            puts "collatz_sequence 9 20 collatz_sequence [collatz_sequence 9 20]"
            puts "collatz_sequence 8 15 collatz_sequence [collatz_sequence 8 15]"
            puts "collatz_sequence 7 20 collatz_sequence [collatz_sequence 7 20]"
            puts "collatz_sequence 6 15 collatz_sequence [collatz_sequence 6 15]"
            puts "collatz_sequence 5 15 collatz_sequence [collatz_sequence 5 15]"
            puts "collatz_sequence 4 15 collatz_sequence [collatz_sequence 4 15]"
            puts "collatz_sequence 3 15 collatz_sequence [collatz_sequence 3 15]"
            proc table_format_out  n {
                set sum 0
                ;# initial  level
                set count 1
                puts "%| table|  | printed in|TCL format |% "
                puts "%| number | iteration |partial Collatz_Sequences  |comment, if any|% "
                ;# adapted proc from <RS>
                ;# printout in TCL WIKI format table
                for { set i 1 } { $i <= $n } { incr i } {
                    incr count
                    puts "&| $count | 120   | [ collatz_sequence $count 120 ]| |&"
                    incr sum $i
                }
                return $sum
            }
            table_format_out 31
            ;# following dresses up console output to easy eye
            ;# end of working deck
            ;# add cosmetics below to bottom of file
            console eval {.console config -bg palegreen}
            console eval {.console config -font {fixed 20 bold}}
            console eval {wm geometry . 40x20}
            console eval {wm title . " Report for Collatz_Sequences (3*N+1) Calculator V2  "}
            console eval {. configure -background orange -highlightcolor brown -relief raised -border 30}    

Pseudocode and Equations Section

     #pseudocode can be developed from rules of thumb.
     #pseudocode: some problems can be solved by proportions (rule of three), to some order of magnitude
     #pseudocode: enter quantity1,  quantity2, quantity3 and expected output (quantity4) for testcases.
     #pseudocode: enter time in years, number of remaining items
     #pseudocode: output fraction of (remaining items) over (items at time zero)
     #pseudocode: ouput remaining items as fraction or percent
     #pseudocode: output fraction of (quantity4 ) over ( quantity1 at time zero)
     #pseudocode: output fraction of (quantity2) * (quantity3 ) over (quantity1 at time zero)
     #pseudocode: outputs should be in compatible units.
     #pseudocode: rules of thumb can be 3 to 15 percent off, partly since g..in g..out.
     #pseudocode: need test cases > small,medium, giant
     #pseudocode: need testcases within range of expected operation.
     #pseudocode: are there any cases too small or large to be solved?
     #; Terence Tao realized that the Collatz conjecture was similar to partial differential equations.
     #;  The Tao starting sample weighs  toward numbers that
     #; have a remainder of 1 after being divided by 3,
     #; and away from numbers that have a remainder of 2 after being divided by 3.
     #;  TCL pseudocode
     #;  remove all numbers divisible by 3 on a list of integers, 
     #;  find all numbers that have a remainder of 1, when divided by 3.


Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 12Aug2020

HE 2021-09-11: Hello Gold, I have a bunch of suggestions/questions about the code. Mainly in respect to other operating systems but, also if one is using your code in a win10 or related system. This is not to bother you.

As far as I understand your code is planned to be executed on a command line. There is no input. And, output is completely done by using 'puts'. It also use only commands/procedures provided by a Tcl only installation (beside some lines I will discuss below). That means the code should be possible to execute everywhere where we can call a tclsh and copy or source the code into it.

Poorly this doesn't work. Because the code contains "package require Tk" without using any GUI and some installation will not have a Tk. And using the command 'console' which is only available in wish.exe but not in wish compiled for Linux, for example.

For sure some could simply copy the code between "package require Tk" and the usage of 'console' at the end to try to get it running. That will not work because there is a 'console show' in between. This needs also to be excluded. At the end the user interested of the result of your script needs to investigate the code to get it running. A bit inconvenience from my point of view. Particularly because all the problematic lines are mainly for eye candy only.

How would I try to execute this type of scripts: On win10 I would open a wish. It displays a console window. I would make a copy and past (c&p) of the code into this console window. The output would be crude to read because of:

  • all the output before "proc table_format_out ..." would not be easy to read because the 'puts' lines and the output are interchanging.
  • The change of font and geometry left a console window far to big on some displays and not able to show the text in a readable way. At least not for me.
  • I don't discuss the format of the able output here, because this also not help to read the results.

Next try would be to open a tclsh on win10 and c&p the code into it. The result is similar. The font size and console background can't be changed so it is more comfortable to read. But the other issues mentioned above still exists and in addition we have the error message in between about the 'console' command.

Next try would be to store it in a file, start a tclsh on win10 (because "package require Tk" let me think it is planned to do so) and source the file into it. This simply raise a "Invalid command name "console"". This will be the same on Linux.

Next try would be to start a wish on win10 and source the stored file into it. I think that is the result you wanted. But, eye candy for you on your computer doesn't fit well on my computer. You also don't know if the user will have problems with a green background. And a smaller font size would show more from the table. At least on the computer I tried your code.

Now I tried all four variants on Linux. Sourcing the file lead in both cases to error message "invalid command name "console"" without any other output. C&p into a wish or tclsh lead to the ugly output where it is not clear where to look for the wanted result.

What I would change:

  • To be platform independent I would remove "package require Tk" and the 'console' lines.
  • To be able to run it by double-click inside win10 I would add somewhere at the top of the script:
if {$tcl_platform(platform) == {windows}} {
        console show
}
  • I would put all output into one procedure so that the last command could this procedure and afterwards all output is generated.

Sorry again, for all the comments only because of output and executing. I tend to write to much in case I try to explain my view. But look how easy it is to make the code run in 8 different conditions instead only running under one condition.

Until here, I read so often this piece of code that I found some small other items:

  • Procedures isPositive and isNegative are not used. Perhaps, isPositive was planned to be used in collatz_sequence as a protection not to use negative numbers?
  • "set tclprecision 17" is used. The Tcl 8.6 documentation tells "It defaults to 0. Applications should not change this value;". I'm not sure to understand why it is used. The whole math of that problem should be integer calculation. At least until the limit of 8 byte integer is crossed. And as long as no fraction is forced the math should stay integer. Perhaps, I'm wrong. But, that is I understood how Tcl calculation works.
  • Where you are using the math packages you load in the beginning? I can't find the usage of it. I would understand "package require math::bignum" to be able to calculate behind the 8 byte integer limit. But the named one?

HE 2021-09-12: Since my comment yesterday, all comment lines are changed from '#' to ';#' at the beginning. Is there a reason for this? From my knowledge a ';' is not need for a proper comment at the beginning of a line. Only if the comment is behind a command on the same line a ';' is needed before the '#' to separate both commands.

HE 2021-09-12: As an answer of your question on Ask, and it shall be given # 13 here my version of collatz_sequence based on your one. For loop provide everything to control iteration. Some tests in the beginning to assure that positive integers are used for number and iteration. That makes it easier to stop as we find 1 as a result. At least the first 10 million integer I tried to use for number will all end with 1 and then go to the endless sequence. As far as I understood others are tried far higher numbers with the same end. Also I would not check separately for odd and even. One check is enough. And I used the expression itself instead of put it into a separate procedure. This will spare calculation power with bigger numbers.

proc collatz_sequence {number iteration} {
        # Check parameter
        if {!([string is wideinteger $number] && $number > 0)} {
                error "Number '$number' is not positiv integer!"
        }
        if {!([string is wideinteger $iteration] && $iteration > 0)} {
                error "Iteration '$iteration' is not positiv integer!"
        }
        # Initialisation
        set sequence [list $number]

        # Interations
        for {set n 0} {$n <= $iteration} {incr n} {
                if {$number == 1} {
                        return $sequence
                }
                if {$number % 2} {
                        # Odd
                        set number [expr {(3 * $number) + 1}]
                } else {
                        set number [expr {$number / 2}]
                }
                lappend sequence $number
        }
        # After value reach 1 it is a endless sequence of 4 2 1 4 2 1 ...
        # If we reach not 1 during the given ammount of iteration
        # we raise an error to signal that number of iterations are to small
        # or we reach the case all looking for.
        error "Number of iteration '$iteration' reached without getting 1!"
}

xx