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

Updated 2021-09-11 13:34:00 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 collatz_sequence proc returns the collatz_sequence from positive integers. This program can easily generate an endless loop. 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.


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.


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
  • 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


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} {
            set sequence  [ list ]
            set count 0
            set val $number
            set skip 0
            lappend sequence $val
            while {$count < $iteration} {
                if { $val == $skip   } { lappend sequence  1 ; break }
                
                if { [ IsEven $val ] } { set val [ expr { $val / 2 } ] ; lappend sequence $val  }
                if { [ IsOdd $val ] } { set val [ expr { (3 *  $val ) + 1 } ]; lappend sequence $val   }
                incr count
                if { $count == $iteration   } { break }
                set skip 2
            }
            return $sequence
        }
        console show
        puts "  Console wrapper for solution proc"
        puts "  ***************************"
        puts "  ***************************"
        puts "collatz_sequence 31 15 collatz_sequence [collatz_sequence 31 15]"
        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 15 collatz_sequence [collatz_sequence 9 15]"
        puts "collatz_sequence 8 15 collatz_sequence [collatz_sequence 8 15]"
        puts "collatz_sequence 7 15 collatz_sequence [collatz_sequence 7 15]"
        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 | 5   | [ collatz_sequence $count 5 ]| |&"
                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}         




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

x