***Collatz_Sequences (3*N+1) in Console Example Demo for TCL V2 ***[gold] 9/29/2021 No longer updated. Please destroy page. Thank youThis 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 ---- <<TOC>> ***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. ---- The logic order here for Collatz_Sequence is if odd, then N = ((3*$N+1)/3) and if even, then N = ( $N / 2 ). Others put logic as if $N even, return { $N / 2 } and 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. ---- ====== # formula for Collatz_Sequence # By custom, the end of the partial collatz_sequence # is 1 or start of repetitive integers. # if odd, then N = ((3*$N+1)/2) # if even, then N = ( $N / 2 ) if {$number % 2} { # Odd ; set number [expr {(3 * $number) + 1}] } else { # Even ; set number [expr {$number / 2}] } ====== ---- The modified_Collatz_Sequence is slightly different 2nd equation ((3*$N+1)/2), used for faster and different numbers resolution as outlined in [https://en.wikipedia.org/wiki/Collatz_conjecture]. The formula for modified_Collatz_Sequence produces different terms, but resolves quicker in fewer terms. The logic is if odd, then N = ((3*$N+1)/2) and if even, then N = ( $N / 2 ). The modified formula in the following code may switched in the main _Collatz_Sequence proc. ---- Returning to the previous example, the collatz_sequence for integer 5 is '''< 5 16 8 4 2 1 >''' Whereas the modified_collatz_sequence for integer 5 is '''< 5 8 4 2 1 >'''. The modified_collatz_sequence offers fewer terms and faster resolution. ---- ====== # formula for modified_Collatz_Sequence # produces different terms, but resolves quicker in fewer terms # By custom, the end of the partial collatz_sequence # is 1 or start of repetitive integers. # if odd, then N = ((3*$N+1)/2) # if even, then N = ( $N / 2 ) if {$number % 2} { # Odd ; set number [expr {((3 * $number) + 1) / 2}] } else { # Even ; set number [expr {$number / 2}] ====== ---- *** 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!" } ====== ---- gold 2021-09-13. Solved problem. The proc collatz_sequence was substantially rewritten by HE and I replaced in the main deck and good feedback comments, which I will have to chew on. Many thanks to [HE]. I checked the proc collatz_sequence in small test suite and proc looks good, so I will pass ticket on to TCLLIB math. I have loaded the ticket e035b93f363dc62153375b77ad3bcf339a68d407 Title: Collatz_Sequence, modified_Collatz_Sequence, posiitve & negative logic test for integers. ---- **Hidden Comments Section** <<discussion>> 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 proving and petitioning the collatz_sequence code to the [TCLLIB]. On my own PC on Windows 10, 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 tap once on the icon (text linked to ActiceTCL) 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, printout, or other wiki style printout? I might point that to test compatibility with [TCLLIB], I do test homebrew code loaded in the local copy of [TCLLIB] math and associated libraries.This is a local setup on Windows10, so references to a my local TCLLIB::math library would not be portable. ---- [gold] 2021-09-13. "And slowly, I turn inch by inch" - Movies. I have swapped ;# for # in comment lines. ----[gold] 2021-09-13. Solved problem. The proc collatz_sequence was substantially rewritten by HE and I replaced in the main deck and good feedback comments, which I will have to chew on. Many thanks to [HE}. I checked the proc collatz_sequence in small test suite and proc looks good, so I will pass ticket on to TCLLIB math. I have loaded the ticket e035b93f363dc62153375b77ad3bcf339a68d407 Title: Collatz_Sequence, modified_Collatz_Sequence, posiitve & negative logic test for integers.[gold] 2021-09-13. Solved problem. The proc collatz_sequence was substantially rewritten by [HE] and I replaced in the main deck and good feedback comments, which I will have to chew on. Many thanks to [HE}. I checked the proc collatz_sequence in small test suite and proc looks good, so I will pass ticket on to TCLLIB math. I have loaded the ticket e035b93f363dc62153375b77ad3bcf339a68d407 Title: Collatz_Sequence, modified_Collatz_Sequence, positive & negative logic test for integers.---- ***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 [https://wiki.tcl-lang.org/page/One+Liners+Programs+Compendium++and+TCL+demo+examples+calculations%2C+numerical+analysis] * 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 [http://tmml.sourceforge.net/doc/tcllib/interpolate.html] * rosettacode.org/wiki/Even_or_odd [https://rosettacode.org/wiki/Even_or_odd#Tcl] * 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. * see modified Collatz formula in en.wikipedia.org/wiki/Collatz_conjecture ---- ---- ***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 in|TCL 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 # Rewritten by HE on TCL WIKI proc collatz_sequence {number iteration} { # 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 positive 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!" } ====== ---- *** 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** <<discussion>> Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, [gold] 12Sep2021 a ---- <<discussion>> Hidden Comments Section ---- ---- <<discussion>> test for hidden comments stop ---- test! seems to work ---- <<discussion>> Hidden Change Log Section ---- [gold] 9/11/2021. first edit ---- [gold] 9/12/2021. replaced original proc with [HE] proc ---- [gold] 9/13/2021. added modified_collatz_sequence formulas, replaced :# with #. ---- [gold] 9/13/2021. forwarded ticket on collatz_sequences to TCLLIB. I have loaded the ticket e035b93f363dc62153375b77ad3bcf339a68d407 Title: Collatz_Sequence, modified_Collatz_Sequence, posiitve & negative logic test for integers. ---- <<categories>> Numerical Analysis | Toys | Calculator | Mathematics| Example| Toys and Games | Games | Application | GUI ---- <<categories>> Development | Concept| Algorithm | Biology