Error processing request

Parameters

CONTENT_LENGTH0
REQUEST_METHODGET
REQUEST_URI/revision/Sample+Math+Programs?V=24
QUERY_STRINGV=24
CONTENT_TYPE
DOCUMENT_URI/revision/Sample+Math+Programs
DOCUMENT_ROOT/var/www/nikit/nikit/nginx/../docroot
SCGI1
SERVER_PROTOCOLHTTP/1.1
HTTPSon
REMOTE_ADDR172.70.100.126
REMOTE_PORT60730
SERVER_PORT4443
SERVER_NAMEwiki.tcl-lang.org
HTTP_HOSTwiki.tcl-lang.org
HTTP_CONNECTIONKeep-Alive
HTTP_ACCEPT_ENCODINGgzip, br
HTTP_X_FORWARDED_FOR3.22.249.158
HTTP_CF_RAY87be9ecd2e8ae254-ORD
HTTP_X_FORWARDED_PROTOhttps
HTTP_CF_VISITOR{"scheme":"https"}
HTTP_ACCEPT*/*
HTTP_USER_AGENTMozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; [email protected])
HTTP_CF_CONNECTING_IP3.22.249.158
HTTP_CDN_LOOPcloudflare
HTTP_CF_IPCOUNTRYUS

Body


Error

Unknow state transition: LINE -> END

-code

1

-level

0

-errorstack

INNER {returnImm {Unknow state transition: LINE -> END} {}} CALL {my render_wikit {Sample Math Programs} These\ sample\ math\ programs\ are\ REALLY\ simple\ examples\ of\ using\ programming\ to\ solve\ math\ problems.\ \ L.\ Prevett,\ a\ Mathematics\ Instructor\ at\ Cochise\ College\ in\ Sierra\ Vista\ Arizona\ (US)\ posted\ a\ request\ on\ the\ seul-edu\ mailing\ list\ \n(\ http://www.seul.org/edu/\ )\ \nasking\ for\ program\ examples.\ \ He\ writes\ \n''I\ think\ the\ reason\ I\ can't\ find\ anything\ like\ this\ is\ that\ it\ is\nTOO\ simple.''\n\nEach\ program\ should\ be\ a\ complete\ sample\ script.\ \ Linux\ users\ should\ be\ able\ to\ save\ the\ code\ to\ a\ file,\ then\ use\ the\ command\ ''tclsh\ filename''\ to\ run\ the\ program.\ \ (Note\ that\ Linux\ users\ might\ have\ to\ type\ ''tclsh<TAB>''\ to\ get\ a\ particular\ version\ of\ tclsh,\ like\ tclsh8.0.)\n\nHere\ is\ a\ list\ of\ simple\ programs\ that\ he\ would\ like\ to\ see.\ \ Can\ you\ help\ write\ one\ of\ them?\ \ I\ think\ the\ goal\ is\ to\ show\ how\ to\ use\ a\ programming\ language\ to\ solve\ a\ problem,\ as\ opposed\ to\ a\ demonstration\ of\ efficient\ algorithms,\ so\ don't\ worry\ too\ much\ about\ tuning\ for\ optimum\ performance.\n\n\ \ \ *\ A\ program\ that\ does\ a\ loop\ and\ prints\ the\ numbers\ from\ 1\ to\ 10\n\ \ \ *\ A\ program\ that\ sums\ the\ numbers\ in\ a\ list\ '''(NEW)''',\ mean,\ square\ mean,\ standard\ deviation\n\ \ \ *\ A\ program\ that\ factors\ a\ number\n\ \ \ *\ A\ program\ that\ generates\ a\ list\ of\ prime\ numbers\ and\ prints\ the\ results\ to\ a\ file\n\ \ \ *\ A\ program\ that\ reads\ a\ list\ of\ numbers\ from\ a\ file\ and\ prints\ their\ factors\n\ \ \ *\ A\ program\ that\ uses\ the\ Euclidean\ Algorithm\ to\ print\ the\ gcd\ of\ two\ numbers\n\ \ \ *\ A\ program\ that\ computes\ a\ factorial\ using\ recursion\n\ \ \ *\ A\ program\ that\ prints\ an\ addition\ table\n\ \ \ *\ A\ program\ that\ prints\ a\ multiplication\ table\n\ \ \ *\ A\ program\ that\ prints\ a\ multiplication\ table\ mod\ n\n\ \ \ *\ A\ program\ that\ prints\ the\ sum\ of\ two\ squares\n\ \ \ *\ A\ program\ that\ inputs\ b,n,\ and\ p\ and\ prints\ b^n\ mod\ p\ for\ n=1\ to\ n-1\n\ \ \ *\ A\ program\ that\ prints\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n.\n\ \ \ *\ A\ program\ that\ computes\ the\ 'osculator'\ for\ a\ prime\ number\ and\ uses\ it\ to\ test\ an\ input\ number\ for\ divisibility\ by\ that\ prime.\n\ \ \ *\ \[Fraction\ math\]\ has\ some\ related\ programs.\n\ \ \ *\ A\ program\ that\ inputs\ a\ number\ and\ a\ counting\ interval\ and\ prints\ the\ Ring\ of\ Josephus.\n\ \ \ *\ A\ program\ that\ prints\ the\ partitions\ of\ a\ number.\n\ \ \ *\ A\ program\ to\ approximate\ pi\ using\ Monte-Carlo\ methods.\n\ \ \ *\ A\ simple\ calculator\ (mathematically\ oriented\ input\ or\ remembered\ functions?)\n\ \ \ *\ A\ program\ that\ computes\ 1000\ digits\ of\ ''e''\ using\ a\ spigot\ algorithm.\n\nAny\ others?\n\n''RWT''\n\n---\n\nFor\ dealing\ with\ complex\ numbers\ see\ \[Complex\ math\ made\ simple\].\n\n---\n\nAfter\ conferring\ with\ RWT\ I\ put\ these\ programs\ along\ with\nmost\ of\ the\ comments\ in\ the\ K-12\ Mathematics\ Teachers\ Guide\ for\ Linux\n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/outline.html\]\nbeing\ developed\ for\ SEUL.\ The\ programs\ are\ in\ the\ \"Programming\ Mathematics\"\ \ \[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/programming_math.html\]\ \nsection\ under\ \"tcl/tk\"\ \n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/tcl/tcltk_math.html\].\ \nThere\ is\ also\ a\ link\ to\ \"Suggested\ Programming\ Problems\ for\ Mathematics\"\ \n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/suggested_progs.html\]\ on\ that\ page.\n\nThanks\ to\ all\ the\ members\ of\ the\ comp.lang.tcl\ newsgroup\ and\ the\ Tcler's\nWiki\ who\ have\ contributed\ programs.\ If\ I\ didn't\ get\ some\ of\ the\ncredits\ right,\ or\ if\ I\ missed\ a\ credit,\ or\ if\ you\ have\ any\ comments\ or\ other\ issues,\ please\ feel\ free\ to\ contact\ me.\n\nL.\ Prevett,\nMathematics\ Instructor\nCochise\ College,\ Sierra\ Vista,\ AZ,\ US\[email protected],\n11-2-00\n\n----\n\[LV\]\ I\ was\ wondering\ today\ whether\ the\ original\ requestor\ was\ interested\ in\ examples\ that\ demonstrate\ how\ one\ can\ make\ use\ of\ fancy\ tcl\ tricks\ to\ get\ the\ fastest\ answers,\ examples\ that\ demonstrate\ the\ underlying\ mathematical\ concepts,\ or\ just\ examples\ of\ any\ code\ that\ provides\ the\ reader\ with\ answers\ (and\ who\ cares\ how\ or\ why\ the\ program\ works)?\ \n\n----\n\n**A\ program\ that\ does\ a\ loop\ and\ prints\ the\ numbers\ from\ 1\ to\ 10**\n======\n\ #\ A\ program\ that\ does\ a\ loop\ and\ prints\ \n\ #\ the\ numbers\ from\ 1\ to\ 10\n\ #\n\ set\ n\ 10\ \n\ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ puts\ \$i\n\ \}\n======\nor\ alternatively\n======\n\ #\ A\ program\ that\ does\ a\ loop\ and\ prints\ \n\ #\ the\ numbers\ from\ 1\ to\ 10\n\ #\n\ set\ i\ 1\n\ while\ \{\$i\ <=\ 10\}\ \{\n\ \ \ \ \ puts\ \$i\n\ \ \ \ \ incr\ i\n\ \}\n======\nThese\ are\ just\ like\ in\ C.\ Another,\ Tcl-only\ alternative\ for\ short\ lists:\n======\n\ foreach\ i\ \{1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9\ 10\}\ \{puts\ \$i\}\ \;#RS\n======\n----\n\n**A\ program\ that\ sums\ the\ numbers\ in\ a\ list**\n======\n\ proc\ lsum\ L\ \{expr\ \[join\ \$L\ +\]+0\}\n======\nThis\ brief\ little\ beauty\ does\ no\ I/O\ (which\ should\ be\ separated\ anyway).\ In\ an\ interactive\ tclsh,\ the\ last\ result\ is\ automatically\ output.\ So\ you\ can\ test\ it\ with\n======\n\ lsum\ \{1\ 2\ 3\ 4.5\}\n\ %\ 10.5\n======\nThis\ very\ Tcl'ish\ style\ concatenates\ the\ elements\ of\ L\ with\ a\ plus\ sign,\ which\ then\ looks\ like\ ''1+2+3+4.5'',\nappends\ +0\ to\ the\ end\ (just\ in\ case\ the\ list\ was\ empty)\ and\ has\ the\ resulting\ string\ evaluated\ by\ expr\ (and\ as\ DKF\ measured,\ this\ is\ pretty\ efficient\ -\ see\ \[Lambda\ in\ Tcl\]).\ As\ the\ ''expr''\ is\ the\ (first,\ only,\ and)\ last\ command\ in\ the\ proc,\ an\ explicit\ ''return''\ is\ not\ needed.\ Again\ for\ conciseness,\ a\ one-element\ arg\ list\ for\ proc\ need\ not\ be\ braced.\nWhile\ we're\ at\ it,\ the\ arithmetic\ average\ can\ be\ done\ with\n======\n\ proc\ lavg\ L\ \{expr\ (\[join\ \$L\ +\])/\[llength\ \$L\].\}\n======\nWe\ don't\ have\ to\ provide\ for\ empty\ lists\ here,\ since\ we\ can't\ divide\ by\ a\ length\ of\ 0...\ See\ \[Steps\ towards\ functional\ programming\]\ on\ how\ to\ use\ this\ in\ powerful\ one-liners.\ -\ As\ I\ just\ needed\ them,\ here's\ '''square\ mean'''\ and\ '''standard\ deviation'''\ also:\n======\n\ proc\ mean2\ list\ \{\n\ set\ sum\ 0\n\ foreach\ i\ \$list\ \{set\ sum\ \[expr\ \{\$sum+\$i*\$i\}\]\}\n\ expr\ \{double(\$sum)/\[llength\ \$list\]\}\n\ \}\n\ proc\ stddev\ list\ \{\n\ set\ m\ \[lavg\ \$list\]\n\ expr\ \{sqrt(\[mean2\ \$list\]-\$m*\$m)\}\n\ \}\ \;#\ RS\n======\n----\n#\ mad\ -\ I\ tried\ the\ above\ lsum\ implementation\ with\ large\ lists\ having\ elements\ in\ excess\ of\ 30,000.\ It\ gets\ very\ slow\ and\ results\ in\ segmentation\ fault.\ So,\ take\ a\ judgement\ on\ based\ on\ your\ use\ case.\n----\n**A\ program\ that\ factors\ a\ number**\n======\n\ #\ A\ program\ that\ factors\ a\ number\n\ puts\ -nonewline\ \"Enter\ number\ to\ factor:\ \ \"\n\ set\ num\ \[gets\ stdin\]\n\n\ for\ \{set\ i\ \$num\}\ \{\$i>0\}\ \{incr\ i\ -1\}\ \{\n\ \ \ \ \ if\ \{\ \$num\ %\ \$i\ ==\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \$i\n\ \ \ \ \ \}\n\ \}\n======\nMS:\ I\ understand\ \"factor\ a\ number\"\ as\ \"producing\ the\ list\ of\ prime\ \nfactors\"\;\ the\ algorithm\ above\ produces\ instead\ a\ list\ of\ all\ integer\ndivisors\ (after\ cleaning\ up\ the\ syntax\ ...)\n\nSo,\ I\ propose\ instead:\n======\n\ #\ A\ program\ that\ factors\ a\ number\n\ proc\ factor_num\ \{num\}\ \{\n\ \ \ \ \ for\ \{set\ i\ 2\}\ \{\$i\ <=\ \$num\}\ \{\}\ \{\n\ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$num\ %\ \$i\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ lappend\ factors\ \$i\n\ \ \ \ \ \ \ \ \ \ \ set\ num\ \[expr\ \{\$num\ /\ \$i\}\]\n\ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ incr\ i\n\ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$factors\n\ \}\n\ \ \ \ \ \n\ proc\ factor_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Enter\ number\ to\ factor:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ num\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\ \[expr\ \{int(abs(\$num))\}\]\ !=\ \$num\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \"Sorry:\ only\ non-negative\ integers\ allowed\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ set\ factors\ \[factor_num\ \$num\]\n\ \ \ \ \ \ \ \ \ puts\ \"\$num\ =\ \[join\ \$factors\ \{\ *\ \}\]\"\n\ \ \ \ \ \}\n\ \}\n\n\ factor_run\n\ #\ end\ of\ program\n======\nMore\ efficient\ for\ large\ numbers\ would\ be\n======\n\ proc\ factor_num\ \{num\}\ \{\n\ \ \ \ \ for\ \{set\ i\ 2\}\ \{\$i\ <=\ \$num\}\ \{\}\ \{\n\ \ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$num\ %\ \$i\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ factors\ \$i\n\ \ \ \ \ \ \ \ \ \ \ \ \ set\ num\ \[expr\ \{\$num\ /\ \$i\}\]\n\ \ \ \ \ \ \ \ \ \}\ elseif\ \{\[expr\ \$i*\$i\ >\ \$num\]\}\ \{\n\ \ \ \ \ \ lappend\ factors\ \$num\n\ \ \ \ \ \ break\n\ \ \}\ elseif\ \{\$i\ !=\ 2\}\ \{\ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ incr\ i\ 2\n\ \ \}\ else\ \{\n\ \ \ \ \ \ set\ i\ 3\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$factors\n\ \}\n======\n----\n\n''''italic\ text''''**A\ program\ that\ generates\ a\ list\ of\ prime\ numbers\ and\ prints\ the\ results\ to\ a\ file**\n======\n\ proc\ all_primes\ \{max\}\ \{\n\ \ \ \ \ set\ primes\ \[list\ 2\]\n\ \ \ \ \ for\ \{set\ test\ 3\}\ \{\$test\ <=\ \$max\}\ \{incr\ test\ 2\}\ \{\n\ \ \ \ \ \ \ \ \ set\ maxTest\ \[expr\ \{int(sqrt(\$test))\}\]\n\ \ \ \ \ \ \ \ \ foreach\ prime\ \$primes\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$prime\ \ >\ \$maxTest\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ primes\ \$test\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break\n\ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$test\ %\ \$prime\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break\n\ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$primes\n\ \}\n\ \ \ \ \ \ \n\ proc\ all_primes_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Compute\ all\ primes\ below:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ max\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\$max\ <\ 2\}\ \{\n\ \ \ \ \ \ \ \ \ set\ max\ 2\n\ \ \ \ \ \}\n\ \ \ \ \ set\ primes\ \[all_primes\ \$max\]\n\n\ \ \ \ \ set\ f\ \[open\ \"primes_below_\$max\"\ w\]\n\ \ \ \ \ puts\ \$f\ \$primes\n\ \ \ \ \ close\ \$f\n\ \}\n\n\ all_primes_run\n\ #\ end\ of\ program\n======\n----\n\n**A\ program\ that\ reads\ a\ list\ of\ numbers\ from\ a\ file\ and\ prints\ their\ factors.**\n======\n\ #\ Start\ by\ including\ one\ of\ the\ factor_num\ procedures\n\ #\ defined\ above,\ then\ just\ read\ a\ file\ print\ the\ factors.\n\ set\ f\ \[open\ \"test.dat\"\ r\]\n\ while\ \{\ !\ \[eof\ \$f\]\ \}\ \{\n\ \ \ \ scan\ \[gets\ \$f\]\ %i\ number\n\ \ \ \ set\ factors\ \[factor_num\ \$number\]\n\ \ \ \ puts\ \"\$number:\ \ \$factors\"\n\ \}\n\ close\ \$f\n======\n\n----\n\n**A\ program\ that\ uses\ the\ Euclidean\ Algorithm\ to\ print\ the\ GCD\ (greatest\ common\ divisor)\ of\ two\ numbers**\n======\n\ proc\ gcd\ \{num1\ num2\}\ \{\n\ \ \ \ while\ \{\[set\ tmp\ \[expr\ \{\$num1%\$num2\}\]\]\}\ \{\n\ \ \ \ \ \ \ set\ num1\ \$num2\n\ \ \ \ \ \ \ set\ num2\ \$tmp\n\ \ \ \ \}\n\ \ \ \ return\ \$num2\n\ \}\n======\n\n----\n\n**A\ program\ that\ computes\ a\ factorial\ using\ recursion***\n======\n\ proc\ fact\ \{num\}\ \{\n\ \ \ \ \ if\ \{\$num\}\ \{\n\ \ \ \ \ \ \ \ \ return\ \[expr\ \{\$num\ *\ \[fact\ \[incr\ num\ -1\]\]\}\]\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ return\ 1\n\ \ \ \ \ \}\n\ \}\n\n\ proc\ fact_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Compute\ the\ factorial\ of:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ num\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\ \[expr\ \{int(abs(\$num))\}\]\ !=\ \$num\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \"Sorry:\ only\ non-negative\ integers\ allowed\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ puts\ \[fact\ \$num\]\n\ \ \ \ \ \}\n\ \}\n\n\ fact_run\n======\n''DKF''\ -\ It\ is\ more\ efficient\ to\ use\ iteration:\n======\n\ proc\ fact\ \{n\}\ \{\n\ \ \ \ set\ f\ 1\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ f\ \[expr\ \{\$f\ *\ \$i\}\]\n\ \ \ \ \}\n\ \ \ \ return\ \$f\n\ \}\n======\n----\n\n**A\ program\ that\ prints\ some\ mathematical\ operation\ tables!**\n======\n\ proc\ table\ \{\ op\ N\ \}\ \{\n\ \ \ \ \ \ set\ table\ \"\$op\ table\\n\"\n\ \ \ \ \ \ for\ \{\ set\ i\ 1\ \}\ \{\ \$i\ <=\ \$N\ \}\ \ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ lappend\ line\ \$i\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ set\ i\ 0\n\ \ \ \ \ \ set\ magic\ \{\}\n\ \ \ \ \ \ switch\ -exact\ --\ \$op\ \{\n\ \ \ \ \ \ \ \ \ +\ \{\ set\ i\ -2\ \}\n\ \ \ \ \ \ \ \ \ /\ \{\ set\ magic\ .0\ \}\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ while\ \{\ \[\ incr\ i\ \]\ <=\ \$N\ \}\ \{\n\ \ \ \ \ \ \ \ \ foreach\ item\ \$line\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ append\ table\ \"\ \[\ expr\ \$item\$op\$i\$magic\ \]\"\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ append\ table\ \"\\n\"\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ set\ table\n\ \}\n\n\ foreach\ op\ \{\ *\ +\ /\ -\ ==\ !=\ >\ >=\ <\ <=\ \}\ \{\n\ \ \ \ puts\ \[\ table\ \$op\ 10\ \]\n\ \}\n======\n''-PSE''\n----\n\n**A\ program\ displaying\ any\ expression\ table\ (wish\ version)**\n======\n\ #\ a\ program\ displaying\ any\ expression\ table\ (FP)\n\n\ set\ first\ 0\n\ set\ last\ 9\n\ set\ expr\ \{\$a+\$b\}\n\ #\ can\ be\ replaced\ interactively\ by\ \{\$a*\$b\},\ \{(\$a*\$b)%7)\},\ ...\n\n\ set\ LP\ lp\ \ \;#\ machine\ dependent\ print\ command\n\ \n\ proc\ showTable\ \{first\ last\ oper\}\ \{\n\ \ \ \ \ set\ terms\ \[list\]\n\ \ \ \ \ for\ \{set\ i\ \$first\}\ \{\$i\ <=\ \$last\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ \ \ lappend\ terms\ \$i\n\ \ \ \ \ \}\n\ \ \ \ \ set\ n\ \[llength\ \$terms\]\n\ \ \ \ \ \n\ \ \ \ \ set\ hdx\ 12\n\ \ \ \ \ set\ hdy\ 11\n\ \ \ \ \ set\ bx\ 20\n\ \ \ \ \ set\ by\ 20\n\ \ \ \ \ \n\ \ \ \ \ .c\ delete\ all\n\ \ \ \ \ .c\ configure\ -width\ \[expr\ \{2*(\$n+1)*\$hdx\ +\ 2*\$bx\}\]\ -height\ \[expr\ \{(2*\$n+4)*\$hdy\ +\ 2*\$by\}\]\n\ \ \ \ \ set\ ia\ 0\n\ \ \ \ \ foreach\ a\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ incr\ ia\n\ \ \ \ \ \ \ \ \ set\ ib\ 0\n\ \ \ \ \ \ \ \ \ foreach\ b\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ incr\ ib\n\ \ \ \ \ \ \ \ \ \ \ \ \ set\ r\ \[expr\ \$oper\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{(2*\$ib+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{(2*\$ia+1)*\$hdy\ +\ \$by\}\]\ -text\ \$r\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ .c\ create\ text\ \[expr\ \{(\$n+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{(2*\$n+4)*\$hdy\ +\ \$by\}\]\ -text\ \$oper\n\ \ \ \ \ \n\ \ \ \ \ set\ ia\ 0\n\ \ \ \ \ foreach\ a\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ incr\ ia\n\ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{(2*\$ia+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{\$by\ +\ \$hdy\}\]\ -text\ \$a\n\ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{\$bx\ +\ \$hdx\}\]\ \[expr\ \{(2*\$ia+1)*\$hdy\ +\ \$by\}\]\ -text\ \$a\n\ \ \ \ \ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ (2*\$ia+2)*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n\ +\ 2)*\$hdx\}\]\ \[expr\ \{\$by\ +\ (2*\$ia+2)*\$hdy\}\]\n\ \ \ \ \ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$ia+2)*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$ia+2)*\$hdx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\n\ \ \ \ \ \}\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdx\}\]\ \[expr\ \{\$by\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\ -width\ 2\n\ \}\n\ \n\ canvas\ .c\n\ pack\ .c\n\ \n\ frame\ .f\n\ label\ .f.l1\ -text\ from:\n\ label\ .f.l2\ -text\ to:\n\ entry\ .f.e1\ -textvariable\ first\ -width\ 2\n\ entry\ .f.e2\ -textvariable\ last\ -width\ 2\n\ label\ .f.l3\ -text\ \{expr:\}\n\ entry\ .f.e3\ -textvariable\ expr\n\ pack\ .f.l1\ .f.e1\ .f.l2\ .f.e2\ -side\ left\n\ pack\ .f.e3\ .f.l3\ -side\ right\n\ pack\ .f\ -fill\ x\n\ \n\ button\ .bp\ -text\ print\ -command\ \{exec\ \$LP\ <<\ \[.c\ postscript\]\}\ \n\ button\ .bq\ -text\ exit\ -command\ exit\ \n\ pack\ .bq\ .bp\ -side\ left\n\n\ button\ .b\ -text\ show\ -command\ \{showTable\ \$first\ \$last\ \$expr\}\n\ pack\ .b\ -side\ right\ -expand\ y\ -fill\ x\n\ .b\ invoke\n\n\ #\ end\ of\ program\n======\n----\n\n**A\ program\ that\ prints\ a\ multiplication\ table**\n======\n\ #\ A\ program\ to\ print\ a\ multiplication\ table\ to\ the\ screen\n\n\ proc\ times_table\ \{\ x\ \}\ \{\n\ \ \ \ puts\ \"Multiplication\ table\ for\ \$x.\"\n\ \ \ \ for\ \{set\ i\ 1\ \}\ \{\ \$i\ <\ 10\}\ \{incr\ i\ \}\ \{\n\ \ \ \ \ \ \ set\ answer\ \[expr\ \$x\ *\ \$i\]\n\ \ \ \ \ \ \ puts\ \"\$x\ times\ \$i\ =\ \ \$answer\"\n\ \ \ \ \}\n\ \}\n\n\ proc\ run_table\ \{\ \}\ \{\n\ \ \ \ puts\ -nonewline\ \"Enter\ a\ number:\ \ \"\n\ \ \ \ flush\ stdout\n\ \ \ \ set\ x\ \[gets\ stdin\]\n\ \ \ \ times_table\ \$x\n\ \}\n\n\ run_table\n\ #end\ of\ program\n======\n----\n\n**A\ program\ that\ prints\ a\ multiplication\ table\ mod\ n**\n\nI\ noticed\ that\ the\ 'any\ expression'\ table\ script\ above\ \nwill\ print\ a\ multiplication\ (or\ addition,\ etc)\ table\ mod\ ''n''.\n\nFor\ example,\ \n\n\ \ from:\ 1\ to:\ 4\ expr:\ (\$a\ *\ \$b)\ %\ 5\n\nprints\ the\ multiplication\ table\ mod\ 5.\n\nBy\ the\ way,\ these\ scripts\ are\ really\ great!\n\n''lp''\n\n----\n\n**A\ program\ that\ prints\ the\ sum\ of\ two\ squares**\n======\n\ proc\ sum_of_two_squares\ \{\ x\ y\ \}\ \{\n\ \ \ \ set\ answer\ \[expr\ \$x*\$x\ +\ \$y*\$y\]\n\ \ \ \ puts\ \$answer\n\ \}\n======\n''so''\n----\n\n**A\ program\ that\ inputs\ b,n,\ and\ p\ and\ prints\ b^n\ mod\ p\ for\ n=1\ to\ n-1**\n\n\[KBK\]\ (4\ Oct\ 2000):\n======\n\ proc\ modular_exponent_table\ \{\ b\ p\ \}\ \{\n\ \ \ \ set\ value\ 1\n\ \ \ \ for\ \{set\ n\ 1\}\ \{\$n\ <\ \$p\}\ \{incr\ n\}\ \{\n\ \ \ \ \ \ \ set\ value\ \[expr\ \{(\$value\ *\ \$b)\ %\ \$p\}\]\n\ \ \ \ \ \ \ puts\ \"(\ \$b\ **\ \$n\ )\ mod\ \$p\ ==\ \$value\"\n\ \ \ \ \}\n\ \}\;#\ edited\ for\ clarity\ and\ style\ by\ DKF\n\n\ puts\ -nonewline\ stdout\ \"Enter\ base\ b:\ \"\n\ flush\ stdout\n\ gets\ stdin\ b\n\ puts\ -nonewline\ stdout\ \"Enter\ modulus\ p:\ \"\n\ flush\ stdout\n\ gets\ stdin\ p\n\n\ modular_exponent_table\ \$b\ \$p\n======\nRunning\ the\ above\ gives:\n======\n\ %\ tclsh83\ prime_power.tcl\n\ Enter\ base\ b:\ 3\n\ Enter\ modulus\ p:\ 17\n\ (\ 3\ **\ 1\ )\ mod\ 17\ ==\ 3\n\ (\ 3\ **\ 2\ )\ mod\ 17\ ==\ 9\n\ (\ 3\ **\ 3\ )\ mod\ 17\ ==\ 10\n\ (\ 3\ **\ 4\ )\ mod\ 17\ ==\ 13\n\ (\ 3\ **\ 5\ )\ mod\ 17\ ==\ 5\n\ (\ 3\ **\ 6\ )\ mod\ 17\ ==\ 15\n\ (\ 3\ **\ 7\ )\ mod\ 17\ ==\ 11\n\ (\ 3\ **\ 8\ )\ mod\ 17\ ==\ 16\n\ (\ 3\ **\ 9\ )\ mod\ 17\ ==\ 14\n\ (\ 3\ **\ 10\ )\ mod\ 17\ ==\ 8\n\ (\ 3\ **\ 11\ )\ mod\ 17\ ==\ 7\n\ (\ 3\ **\ 12\ )\ mod\ 17\ ==\ 4\n\ (\ 3\ **\ 13\ )\ mod\ 17\ ==\ 12\n\ (\ 3\ **\ 14\ )\ mod\ 17\ ==\ 2\n\ (\ 3\ **\ 15\ )\ mod\ 17\ ==\ 6\n\ (\ 3\ **\ 16\ )\ mod\ 17\ ==\ 1\n======\n\[KBK\]\ (10\ Oct\ 2000):\ I'm\ not\ sure\ that\ I\ like\ the\ change\ that\n''DKF''\ made.\ \ Let's\ follow\ this\ up\ a\ little\ bit\ on\ \[Tcl\ style\ discussion\].\n----\n\n**A\ program\ that\ computes\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n**\n======\n\ #\ A\ program\ to\ compute\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n\n\n\ proc\ sumto\ \{n\}\ \{\n\ \ \ \ set\ sum\ 0\n\ \ \ \ set\ i\ 1\n\ \ \ \ while\ \{\$i\ <=\ \$n\}\ \{\n\ \ \ \ \ \ \ set\ sum\ \[expr\ \$sum\ +\ \$i\]\n\ \ \ \ \ \ \ incr\ i\n\ \ \ \ \}\n\ \ \ \ puts\ \"The\ sum\ of\ the\ numbers\ from\ 1\ to\ \$n\ is\ \ \$sum\ \"\n\ \}\n\n\ puts\ -nonewline\ stdout\ \"Enter\ a\ number:\ \"\n\ flush\ stdout\n\ gets\ stdin\ n\n\ sumto\ \$n\n\n\ #\ end\ of\ program\n\ #\ I'm\ no\ programmer,\ but\ I\ wanted\ to\ see\ if\ I\ could\ use\ the\ examples\n\ #\ on\ this\ page\ to\ write\ this\ program.\ I\ think\ it\ works\ ...\n======\n''lp''\n\n''Minor\ style\ note''\ -\ It\ is\ better\ to\ use\ the\ \[\[for\]\]\ command\ for\ the\ style\ of\ loop\ you\ were\ doing\ above\ as\ it\ is\ easier\ to\ understand\ the\ purpose\ of\ the\ loop\ when\ you\ come\ back\ to\ look\ at\ it\ later\;\ code\ has\ this\ way\ of\ lasting\ far\ longer\ than\ you\ intended...\ \ '''DKF'''\n\nRS\ --\ Also,\ a\ proc\ should\ return\ the\ result\ rather\ than\ puts'ing\ it.\ So\ here's\ my\ version:\n======\n\ proc\ sumto\ n\ \{\n\ \ \ \ \ set\ sum\ 0\n\ \ \ \ \ for\ \{\ set\ i\ 1\ \}\ \{\ \$i\ <=\ \$n\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ \ incr\ sum\ \$i\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$sum\n\ \}\n======\n\[KBK\]\ --\ I\ edited\ this\ (18\ October\ 2000)\ to\ spare\ Donal's\ tender\ sensibilities\ (see\ \[Tcl\ style\ discussion\]).\n\nJCE\ -\ so\ why\ not\ just\ this:\n======\n\ proc\ sumto\ n\ \{\n\ \ \ \ \ expr\ \$n\ *\ (\$n\ +\ 1)\ /\ 2\n\ \}\n======\n----\n\n**A\ program\ that\ computes\ the\ osculator\ for\ a\ prime\ number,\ and\ uses\ it\ to\ test\ an\ input\ number\ for\ divisibility\ by\ that\ prime**\n======\n\ #\ I\ thought\ the\ group\ might\ be\ interested\ in\ this\ problem.\n\ #\ This\ would\ be\ a\ more\ challenging\ program\ for\ students\ to\n\ #\ program\ because\ they\ have\ to\ access\ individual\ digits\ in\n\ #\ numbers\ to\ form\ new\ numbers.\n\n\ #\ Numbers\ that\ end\ in\ even\ numbers\ or\ 5\ are\ easy\ to\ factor.\n\ #\ Numbers\ that\ end\ in\ 1,3,7,9\ are\ generally\ more\ difficult.\n\n\ #\ Divisibility\ tests\ exist\ for\ numbers\ that\ end\ in\n\ #\ 1,3,7,\ or\ 9,\ but\ they\ use\ a\ special\ 'osculator'.\n\ #\ For\ example,\ the\ osculator\ for\ 7\ is\ 5.\ To\ test\ 343\ for\n\ #\ divisibility\ by\ 7,\ here's\ what\ you\ do:\n\ #\ \ \ multiply\ the\ rightmost\ digit\ by\ the\ osculator,\ 5:\ \ 3x5\ =\ 15\n\ #\ \ \ add\ the\ result\ to\ all\ the\ digits\ to\ the\ left\ of\ 3:\ 34\ +\ 15\ =\ 49\n\ #\ since\ 49\ is\ divisible\ by\ 7,\ 343\ will\ be\ divisible\ by\ 7.\ \n\n\ #\ To\ compute\ the\ osculator\ for\ any\ number,\ first\ multiply\n\ #\ by\ (the\ smallest)\ number\ that\ gives\ a\ product\ whose\ rightmost\n\ #\ digit\ is\ '9'.\ Drop\ the\ '9'\ and\ add\ 1\ to\ all\ the\ remaining\ digits.\n\ #\ The\ osculator\ for\ 71\ is\ 64:\ 71\ x\ 9\ =\ 639,\ 63\ +\ 1\ =\ 64.\n\ #\ The\ osculator\ for\ 23\ is\ \ 7:\ 23\ x\ 3\ =\ \ 69,\ \ 6\ +\ 1\ =\ 7.\n\ #\ The\ osculator\ for\ 17\ is\ 12:\ 17\ x\ 7\ =\ 119,\ 11\ +\ 1\ =\ 12.\n\ #\ The\ osculator\ for\ 19\ is\ \ 2:\ 19\ x\ 1\ =\ \ 19,\ \ 1\ +\ 1\ =\ 2.\n\n\ #\ Another\ example.\ Is\ 3534\ divisible\ by\ 19?\n\ #\ The\ osculator\ for\ 19\ is\ 2.\n\ #\ 4\ x\ 2\ =\ 8,\ 353\ +\ 8\ =\ 361\n\ #\ 1\ x\ 2\ =\ 2,\ \ 36\ +\ 2\ =\ \ 38\n\ #\ Since\ 38\ is\ divisible\ by\ 19,\ 3534\ is\ divisible\ by\ 19.\n\n\ #\ Incidentally,\ if\ the\ sum\ of\ the\ digits\ of\ a\ number\ sum\ to\ '3'\n\ #\ then\ the\ number\ itself\ is\ divisible\ by\ 3.\ This\ is\ because\ the\n\ #\ osculator\ for\ 3\ is\ '1':\ 3\ x\ 3\ =\ 09,\ 0\ +\ 1\ =\ 1.\n======\n''lp''\n\ \n\nYou\ do\ not\ really\ need\ to\ access\ ''individual''\ digits:\ just\ the\ last\ one,\ and\ the\ rest.\ This\ can\ be\ done\ simply\ by\ dividing\ by\ 10\ (modular\ or\ integer\ division).\ For\ instance,\ to\ compute\ the\ osculator\ of\ any\ number,\ just\ do:\n======\n\ proc\ osc\ \{base\}\ \{\n\ \ \ \ for\ \{set\ i\ 1\}\ \{1\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ tmp\ \[expr\ \{\$base\ *\ \$i\}\]\n\ \ \ \ \ \ \ if\ \[expr\ \{(\$tmp\ %\ 10)\ ==\ 9\}\]\ \{\n\ \ \ \ \ \ \ \ \ \ \ return\ \[expr\ \{(\$tmp\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \ \ \ \}\n\ \ \ \ \}\n\ \}\n======\nA\ safer\ version\ is\n======\n\ array\ set\ mult\ \[list\ 1\ 9\ 3\ 3\ 7\ 7\ 9\ 1\]\n\ proc\ osc\ \{base\}\ \{\n\ \ \ \ \ global\ mult\n\ \ \ \ \ set\ last\ \[expr\ \{\$base\ %\ 10\}\]\n\ \ \ \ \ if\ \{\[catch\ \{set\ coeff\ \$mult(\$last)\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ return\ \"Only\ valid\ for\ numbers\ ending\ in\ 1,\ 3,\ 7\ or\ 9\ ...\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ return\ \[expr\ \{((\$base\ *\ \$coeff)\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \ \}\n\ \}\n======\n''MS''\n\n''DKF''\ -\ We\ can\ leverage\ Tcl's\ string/number\ duality\ capabilities\ to\ help\ here.\ \ Note,\ there's\ not\ many\ other\ languages\ where\ you'd\ solve\ the\ problem\ like\ this...\n======\n\ proc\ osc\ base\ \{\n\ \ \ \ if\ \{\[regexp\ \{\[1379\]\$\}\ \$base\ digit\]\}\ \{\n\ \ \ \ \ \ \ set\ coeff\ \[lindex\ \{?\ 9\ ?\ 3\ ?\ ?\ ?\ 7\ ?\ 1\}\ \$digit\]\n\ \ \ \ \ \ \ return\ \[expr\ \{(\$base\ *\ \$coeff\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \}\n\ \ \ \ return\ -code\ error\ \"base\ must\ end\ in\ 1,\ 3,\ 7\ or\ 9\ ...\"\n\ \}\n======\n\n''lp''\ -\ So,\ here's\ a\ mangled\ attempt\ at\ a\ divisbility\ test\ using\ one\ of\ the\ three\ 'osc'\ procs\ above.\n======\n\ #\ perform\ one\ osculation\ of\ n\ by\ m\n\ proc\ oscit\ \{n\ m\}\ \{\n\ \ \ \ \ set\ adder\ \[expr\ \{\$m*(\$n\ %\ 10)\}\]\n\ \ \ \ \ set\ left_digits\ \[expr\ \{int(\$n\ /\ 10)\}\]\n\ \ \ \ \ return\ \[expr\ \{\$left_digits\ +\ \$adder\}\]\n\ \}\n\n\ #\ Test\ the\ divisibilty\ of\ num\ by\ prime.\n\ #\ use\ a\ maximum\ of\ 9\ digits\ for\ the\ number\n\ #\ \ \ \ \ \ \ 123456789\n\ set\ num\ 211111043\n\ set\ prime\ 23\n\n\ #\ get\ the\ osculator\ for\ the\ prime\n\ set\ osc_p\ \[osc\ \[expr\ \$prime\]\]\n\n\ #\ get\ the\ number\ of\ digits\ in\ the\ number\n\ #\ will\ osculate\ the\ number\ this\ many\ times\n\ set\ n_digits\ \[string\ length\ \$num\]\n\n\ #\ compute\ and\ print\ osculations\n\ set\ x\ \$num\n\ for\ \{set\ j\ 1\ \}\ \{\ \$j\ <\ \$n_digits\}\ \{incr\ j\ \}\ \{\n\ \ \ \ puts\ -nonewline\ \"\$x,\ \"\n\ #\ proper\ way\ to\ call\ oscit\ with\ 2\ vars?\n\ \ \ \ set\ x\ \[oscit\ \[expr\ \$x\]\ \[expr\ \$osc_p\]\]\n\ \}\n\ puts\ \"\$x\ ...\"\n\n\ #\ print\ the\ results\n\ puts\ \"\$n_digits\ osculations\ of\ \$num\ by\ osculator\ \$osc_p\ for\ prime\ \$prime.\"\n\ if\ \[expr\ !(\$x\ %\ \$prime)\]\ \{\n\ \ \ puts\ \"Since\ \$x\ is\ divisible\ by\ \$prime,\ \$num\ is\ divisible\ by\ \$prime.\"\n\ \}\ else\ \{\n\ \ \ puts\ \"Since\ \$x\ is\ not\ divisible\ by\ \$prime,\ \$num\ is\ not\ divisible\ by\ \$prime.\"\n\n\ #\ A\ note\ for\ the\ 'else'\ case\ ...\n\ #\ The\ following\ is\ an\ example\ of\ the\ kinds\ of\ patterns\ that\ can\ be\n\ #\ noticed\ in\ mathematics\ with\ the\ use\ of\ a\ computer.\ Such\ patterns\ \ \ \n\ #\ often\ lead\ to\ new\ insights,\ new\ proofs,\ new\ theorems,\ etc.\n\ \ \ puts\ \"Conjecture:\ continued\ osculation\ produces\ a\ list\ of\ numbers\\\ \ \ \n\ \ \ with\ period\ \$prime\ -\ 1\ =\ \[expr\ \$prime\ -\ 1\]:\"\n\ \ \ \ \ \ for\ \{set\ j\ 1\ \}\ \{\ \$j\ <\ \$prime\ \}\ \{incr\ j\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ -nonewline\ \"\$x,\ \"\n\ \ \ \ \ \ \ \ \ set\ x\ \[oscit\ \[expr\ \$x\]\ \[expr\ \$osc_p\]\]\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ puts\ \"\$x\ ...\"\n\ \ \ \ \ \ puts\ \"Conjecture:\ It\ appears\ that\ each\ number\ in\ the\ first\ half\\\ \n\ \ \ \ \ \ added\ to\ its\ conjugate\ in\ the\ second\ half\ is\ divisible\ by\ \$prime.\"\n\ \ \ \}\n\ #\ end\ program\n======\n''lp''\n\n----\n\n**A\ program\ that\ prints\ the\ Ring\ of\ Josephus**\n======\n\ #\ For\ the\ Ring\ of\ Josephus,\ you\ start\ with\ a\ list\ of\ elements\n\ #\ from\ 1\ to\ n,\ and\ a\ counting\ interval,\ k.\n\ #\ You\ make\ one\ pass\ through\ the\ list\ selecting\ each\ kth\ element\n\ #\ in\ the\ list.\ You\ 'delete'\ each\ element\ as\ it\ is\ selected.\n\ #\ You\ continue\ to\ select\ and\ delete\ each\ kth\ element\ until\n\ #\ no\ more\ elements\ remain.\ The\ permutation\ generated\n\ #\ describes\ the\ order\ in\ which\ each\ number\ has\ been\ chosen.\n\n\ #\ First\ selection\ for\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ _\ \ \ _\ \ \ _\ \ \ 1\ \ \ _\ \ \ _\ \ \ _\ \ <--\ order\ chosen\n\n\ #\ Second\ selection,\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ 2\ \ \ _\ \ \ _\ \ \ 1\ \ \ _\ \ \ _\ \ \ _\ \ <--\ order\ chosen\n\n\ #\ etc.\n\n\ #\ For\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ 2\ \ \ 7\ \ \ 6\ \ \ 1\ \ \ 4\ \ \ 3\ \ \ 5\ \ <--\ order\ chosen\ \n\n\ #\ For\ 13\ elements\ and\ a\ counting\ interval\ of\ 3:\ \n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ \ 8\ \ \ 9\ \ \ 10\ \ \ 11\ \ \ 12\ \ \ 13\n\ #\ 11\ \ \ 5\ \ \ 1\ \ \ 8\ \ 10\ \ \ 2\ \ \ 6\ \ 12\ \ \ 3\ \ \ \ 9\ \ \ \ 7\ \ \ \ 4\ \ \ 13\n\n\ #\ This\ is\ called\ the\ Ring\ of\ Josephus\ because,\ as\ one\ version\ of\n\ #\ the\ story\ goes,\ around\ 63\ A.D.\ 13\ soldiers\ formed\ a\ suicide\ pact\n\ #\ instead\ of\ surrendering\ to\ the\ Romans.\ They\ stood\ in\ a\ circle\ and\n\ #\ counted\ around\ by\ 3's\ killing\ each\ man\ in\ turn.\ The\ final\ man\n\ #\ standing\ was\ to\ commit\ suicide.\ Their\ leader,\ Josephus,\ had\n\ #\ cleverly\ chosen\ his\ position\ to\ be\ 'last'.\ Along\ with\ his\ best\n\ #\ friend\ who\ was\ in\ the\ 'next\ to\ last'\ position,\ they\ deserted\n\ #\ to\ the\ Roman\ army.\ \n\n\ #\ I'm\ told\ that\ there\ are\ applications\ in\ coding\ theory\ and\n\ #\ networking,\ but\ I've\ never\ found\ anything\ on\ the\ web.\n\ #\ I\ use\ this\ as\ an\ example\ of\ a\ problem\ that\ cannot\ be\ solved\n\ #\ algebraically.\ Except\ for\ a\ few\ special\ cases\ there\ is\ no\n\ #\ known\ formula\ that\ will\ predict\ when\ an\ element\ will\ be\n\ #\ chosen\ for\ an\ arbitrary\ number\ of\ elements\ and\ counting\ interval.\n\ #\ The\ answer\ must\ be\ determined\ by\ construction\ (programming.)\n======\n''lp''\n======\n\ #\ Program\ to\ generate\ Ring\ of\ Josephus\ by\ DKF\n\ proc\ ROJ\ \{\{n\ 13\}\ \{skip\ 3\}\}\ \{\n\ \ \ \ set\ list\ \{\}\n\ \ \ \ set\ result\ \{\}\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ lappend\ list\ \$i\n\ \ \ \ \}\n\ \ \ \ set\ i\ 0\n\ \ \ \ while\ \{\[llength\ \$list\]\}\ \{\n\ \ \ \ \ \ \ set\ i\ \[expr\ \{(\$i+\$skip)%\[llength\ \$list\]\}\]\n\ \ \ \ \ \ \ lappend\ result\ \[lindex\ \$list\ \$i\]\n\ \ \ \ \ \ \ set\ list\ \[lreplace\ \$list\ \$i\ \$i\]\n\ \ \ \ \}\n\ \ \ \ return\ \$result\n\ \}\n\ proc\ PrintRingOfJosephus\ \{ringSize\ skipNumber\}\ \{\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$ringSize\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ puts\ -nonewline\ \[format\ %4d\ \$i\]\n\ \ \ \ \}\n\ \ \ \ puts\ \"\"\n\ \ \ \ foreach\ i\ \[ROJ\ \$ringSize\ \$skipNumber\]\ \{\n\ \ \ \ \ \ \ puts\ -nonewline\ \[format\ %4d\ \$i\]\n\ \ \ \ \}\n\ \ \ \ puts\ \"\"\n\ \}\n======\n----\n\n**A\ program\ that\ prints\ the\ partitions\ of\ a\ number**\n======\n\ #\ The\ partitions\ of\ a\ number\ are\ simply\ the\ different\ ways\ a\ \n\ #\ number\ can\ be\ summed.\ (Summands\ are\ in\ descending\ order.)\n\ #\ There\ are\ 7\ partitions\ of\ 5:\ \ 5,\ 41,\ 32,\ 311,\ 221,\ 2111,\ 11111\n\ #\ There\ are\ 14\ partitions\ of\ 7:\n\ #\ 7,\ 61,\ 52,\ 511,\ 43,\ 421,\ 4111,\ 322,\ 3211,\n\ #\ 3111,\ 2221,\ 22111,\ 211111,\ 1111111\n\ #\ The\ number\ of\ partitions\ becomes\ large\ very\ quickly.\ Although\n\ #\ simple,\ partitions\ are\ of\ theoretical\ interest\ in\ number\n\ #\ theory\ and\ physics.\n======\n''lp''\n\n\[KBK\]\ (17\ October\ 2000):\ Here's\ one\ possibility:\n======\n\ #----------------------------------------------------------------------\n\ #\n\ #\ part\ --\n\ #\n\ #\ \ \ \ \ \ \ List\ the\ partitions\ of\ a\ number,\ optionally\ limiting\ the\ size\n\ #\ \ \ \ \ \ \ of\ the\ largest\ element.\n\ #\n\ #\ Parameters:\n\ #\ \ \ \ \ \ \ n\ --\ Number\ to\ be\ partitioned\n\ #\ \ \ \ \ \ \ k\ --\ (Optional)\ Size\ of\ the\ largest\ element.\ \ Default\ is\ n.\n\ #\n\ #\ Results:\n\ #\ \ \ \ \ \ \ Returns\ a\ list\ of\ the\ partitions\ of\ n\ with\ no\ element\ larger\n\ #\ \ \ \ \ \ \ than\ k,\ in\ reverse\ lexicographic\ order.\n\ #\n\ #\ Side\ effects:\n\ #\ \ \ \ \ \ \ None.\n\ #\n\ #----------------------------------------------------------------------\n\ \n\ proc\ part\ \{\ n\ \{\ k\ -1\ \}\ \}\ \{\n\ \n\ \ \ \ \ #\ If\ k\ is\ not\ supplied,\ or\ if\ k\ is\ greater\ than\ n\ (no\ partition\n\ \ \ \ \ #\ of\ n\ can\ have\ an\ element\ greater\ than\ n),\ set\ k\ to\ the\ default\n\ \ \ \ \ #\ of\ n.\n\ \n\ \ \ \ \ if\ \{\ \$k\ <\ 0\ ||\ \$k\ >\ \$n\ \}\ \{\n\ \ \ \ \ \ \ \ \ set\ k\ \$n\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ There\ is\ only\ one\ partition\ of\ zero:\ the\ empty\ list.\n\ \n\ \ \ \ \ if\ \{\ \$n\ ==\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ return\ \[list\ \[list\]\]\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ Accumulate\ the\ list\ of\ partitions.\ \ This\ is\ done\ by\ choosing\n\ \ \ \ \ #\ the\ largest\ element\ r\ of\ the\ partition,\ then\ partitioning\n\ \ \ \ \ #\ the\ remaining\ n-r\ so\ that\ no\ element\ is\ greater\ than\ r.\n\ \n\ \ \ \ \ set\ parts\ \{\}\n\ \ \ \ \ for\ \{\ set\ r\ \$k\ \}\ \{\ \$r\ >\ 0\ \}\ \{\ incr\ r\ -1\ \}\ \{\n\ \ \ \ \ \ \ \ \ foreach\ list\ \[part\ \[expr\ \{\ \$n\ -\ \$r\ \}\]\ \$r\]\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ parts\ \[linsert\ \$list\ 0\ \$r\]\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ Return\ the\ accumulated\ list.\n\ \n\ \ \ \ \ return\ \$parts\n\ \}\n\ \n\ #\ Main\ program\ to\ demonstrate\ partitioning\ of\ integers.\n\ \n\ for\ \{\ set\ i\ 0\ \}\ \{\ \$i\ <=\ 7\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ puts\ \"Partitions\ of\ \$i:\"\n\ \ \ \ \ foreach\ list\ \[part\ \$i\]\ \{\n\ \ \ \ \ \ \ \ \ puts\ \$list\n\ \ \ \ \ \}\n\ \}\n======\n----\n**A\ program\ to\ compute\ pi\ using\ Monte-Carlo\ methods**\n======\n\ #\ x^2\ +\ y^2\ =\ 1\ is\ a\ circle\ of\ radius\ 1.\n\ #\ Many\ random\ coordinates\ for\ x\ and\ y\ are\ generated\n\ #\ in\ the\ first\ quadrant\ of\ the\ x-y\ plane\ and\ tested\n\ #\ to\ see\ whether\ the\ points\ fall\ inside\ the\ circle.\n\ #\ The\ area\ in\ quad\ I\ for\ a\ unit\ circle\ is\ pi/4.\n\ #\ The\ ratio\ of\ the\ pairs\ inside\ the\ circle\ to\ the\n\ #\ total\ number\ of\ points\ tested\ approaches\ pi/4\ when\n\ #\ the\ number\ of\ random\ points\ is\ large\ enough.\ \ \n\ \n\ set\ n\ 1000000\n\ set\ count\ 0\n\ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ \ set\ x\ \[expr\ rand()\]\n\ \ \ \ \ \ \ \ set\ y\ \[expr\ rand()\]\n\ \ \ \ \ \ \ \ \ if\ \{\[expr\ \{(\$x*\$x\ +\ \$y*\$y)\ <\ 1\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ incr\ count\ \}\n\ \ \ \ \}\n\ puts\ \"Number\ of\ points\ inside\ the\ circle=\ \ \$count\"\n\ puts\ \"Total\ points\ =\ \$n\"\n\ \n\ set\ pi\ \[expr\ 4e+0*\$count/\$n\]\n\ puts\ \"Approximation\ for\ pi\ is\ \$pi\"\n\ \ \n\ #\ For\ 1,000,000\ points\ pi\ is\ computed\ correct\ to\ 2\ decimal\ places.\n\ #\ Below\ is\ the\ result\ for\ 5\ trials.\ My\ sensibilities\ tell\ me\ that\n\ #\ the\ approximation\ should\ be\ better,\ and\ more\ convergent.\n\ #\ Any\ ideas\ on\ why\ there\ is\ so\ much\ divergence\ in\ the\ results?\n\ #\ Am\ I\ handling\ the\ \[rand\]\ function\ incorrectly?\n\ #\ (Even\ for\ awk,\ there\ is\ 3\ place\ decimal\ accuracy\ for\ n=1M.)\n\ #\ Is\ it\ due\ to\ the\ fact\ that\ rand()\ uses\ the\ clock\ and\n\ #\ does\ not\ generate\ 'true'\ random\ numbers?\n\ #\ Should\ I\ be\ using\ srand()?\n\ #\n\ #\ Also,\ it\ was\ necessary\ to\ use\ \[expr\ 4e+0*\$count/\$n\]\n\ #\ to\ get\ a\ floating\ point\ answer.\ \[expr\ \$count/\$n\]\ gives\ 3.\n\ #\ There\ seem\ to\ be\ some\ subtleties\ in\ this\ simple\ program\n\ #\ that\ I\ don't\ feel\ qualified\ to\ explain.\n\ #\ Any\ thoughts\ would\ be\ appreciated.\n\n\ #\ 5\ trials\ for\ tcl/tk:\n\ #\ points\ inside:\ 785907,\ \ \ 785725,\ 785062,\ \ \ 785194,\ \ \ 785041\n\ #\ approx\ for\ pi:\ 3.143628,\ 3.1429,\ 3.140248,\ 3.140776,\ 3.140164\ \n======\n''lp''\n\n---\n\nSorry\ to\ report:\ your\ sensibilities\ are\ \"wrong\",\ the\ method\ is\ really\ not\ that\ accurate!\n\nIf\ you\ compute\ the\ standard\ deviation\ of\ your\ estimate\ (assuming\ that\ the\ n\ points\ that\ you\ generate\ are\ uniformly\ distributed\ in\ the\ unit\ square),\ you\ obtain\ \n\ \ \ s\ =\ sqrt(pi(4-pi)/n)\ ~\ 1.64/sqrt(n)\n\nIf\ piE\ is\ the\ resulting\ estimate\ for\ n=1E6,\ this\ means\ that\ \n\ \ \ *\ |piE\ -\ pi|\ <\ \ s\ ~\ 1.64E-3\ \ with\ probability\ 0.68\ \n\ \ \ *\ |piE\ -\ pi|\ <\ 2s\ ~\ 3.28E-3\ \ with\ probability\ 0.95\n\ \ \ *\ |piE\ -\ pi|\ <\ 3s\ ~\ 4.92E-3\ \ with\ probability\ 0.997\n\nSo:\ you\ have\ no\ right\ to\ expect\ better\ than\ two\ decimal\ digits\ accuracy\ with\ this\ method\ and\ a\ million\ points.\n\nRemark\ that\ if\ you\ use\ 5\ million\ points\ (i.e.,\ if\ you\ average\ your\ 5\ results),\ the\ resulting\ standard\ deviation\ goes\ down\ to\ \n\ \ s\ ~\ 1.64E-3/sqrt(5)\ ~\ 7.3E-4\ngiving\ a\ reasonable\ probability\ of\ hitting\ the\ third\ decimal\ digit\ ...\n\n''ms''\n\n---\n\nI\ bow\ to\ your\ lamp,\ sir!\ Thanks\ for\ the\ reminder\ to\ check\ the\ math\nbefore\ jumping\ to\ conclusions\ about\ the\ code.\n\nMore\ info\ on\ the\ rand\ function\ is\ here:\ \[rand\]\n\n''lp''\n\n---\n\n''DKF''\ -\ Point\ of\ Tcl\ style:\n\nIt\ is\ better\ to\ write\ the\ above\ code\ as:\n======\n\ proc\ montecarlo-pi\ \{n\}\ \{\n\ \ \ \ set\ count\ 0\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ x\ \[expr\ \{rand()\}\]\n\ \ \ \ \ \ \ set\ y\ \[expr\ \{rand()\}\]\n\ \ \ \ \ \ \ if\ \{(\$x*\$x\ +\ \$y*\$y)\ <\ 1\}\ \{\n\ \ \ \ \ \ \ \ \ \ incr\ count\n\ \ \ \ \ \ \ \}\n\ \ \ \ \}\n\ \ \ \ expr\ \{4e0*\$count/\$n\}\n\ \}\n\ puts\ \"Approximation\ for\ pi\ is\ \[montecarlo-pi\ 1000000\]\"\n======\n----\n**A\ simple\ calculator**\n\n\[Arjen\ Markus\]\ '''A\ simple\ calculator'''\ (two\ types)\ can\ be\ found\ at\ \[A\ little\ math\ language\]\n----\n\n**compute\ first\ thousand\ digits\ of\ e**\n\n\[GS\]\ Here\ is\ a\ small\ program\ that\ computes\ 1000\ digits\ of\ ''e''\ using\ a\ spigot\ algorithm\ (see\ also\ \[e\]):\n======\n\ #\ 1000\ digits\ of\ e\ with\ a\ spigot\ algorithm\n\ \n\ proc\ e1\ \{\}\ \{\n\ \ set\ f\ \{\}\n\ \ set\ n\ 1000\n\ \ set\ b\ \[expr\ \{10\ *\ \$n\ /\ 4\}\]\n\ \ for\ \{set\ i\ 0\}\ \{\$i\ <=\ \$b\}\ \{incr\ i\}\ \{lappend\ f\ 1\}\ \ \ \ \ \n\ \ incr\ n\ -1\n\ \ puts\ -nonewline\ \"2.\"\n\ \ for\ \{set\ j\ 1\}\ \{\$j\ <=\ \$n\}\ \{incr\ j\}\ \{\n\ \ \ \ \ set\ q\ 0\n\ \ \ \ \ for\ \{set\ i\ \$b\}\ \{\$i\ >=\ 1\}\ \{incr\ i\ -1\}\ \{\n\ \ \ \ \ \ \ \ set\ k\ \[expr\ \{\$i\ +\ 1\}\]\n\ \ \ \ \ \ \ \ set\ x\ \[expr\ \{\[lindex\ \$f\ \$i\]*10\ +\ \$q\}\]\n\ \ \ \ \ \ \ \ lset\ f\ \$i\ \[expr\ \{\$x\ %\ \$k\}\]\n\ \ \ \ \ \ \ \ set\ q\ \[expr\ \{\$x\ /\ \$k\}\]\n\ \ \ \ \ \}\n\ \ \ \ \ puts\ -nonewline\ \$q\n\ \ \ \ \ flush\ stdout\n\ \ \}\n\ \}\n\ \n\ e1\n\n======\nReference:\n\n-\ A.\ H.\ J.\ Sale,\ ''The\ Calculation\ of\ e\ to\ Many\ Significant\ Digits'',\ The\ Computer\ Journal\ (1968)\ 11\ (2):\ pp229-230\ \[http://comjnl.oxfordjournals.org/content/11/2/229.full.pdf+html\]\n\n-\ Stanley\ Rabinowitz,\ Stanley\ Wagon,\ ''A\ Spigot\ Algorithm\ for\ the\ Digits\ of\ Pi'',\ American\ Mathematical\ Monthly\ 102\ (3),\ 1995,\ pp195–203\ \[http://www.mathpropress.com/stan/bibliography/spigot.pdf\]\n======\n\n----\n**extended\ eucleian\ algorithm**\n\n\[aleksanteri\]/arezey,\ 17th\ of\ September\ 2008:\ A\ 61LOC\ implementation\ of\ the\ extended\ Euclerian\ Algorithm.\n\n======\n\ proc\ eea\ \{a\ b\}\ \{\n\ \ \ \ \ \ \ \ \ set\ r\ 1\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ x\ \$a\n\ \ \ \ \ \ \ \ \ set\ y\ \$b\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \{\}\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ while\ \{\$r\ !=\ 0\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ r\ \[expr\ \$x%\$y\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ q\ \[expr\ (\$x-\$r)/\$y\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ puts\ \"\$x/\$y\ =\ \$q\ r\ \$r\\t\$x\ =\ \$y*\$q+\$r\"\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ exprs\ \[linsert\ \$exprs\ 0\ \[list\ \$r\ \$x\ \$y\ \$q\]\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$r\ ==\ 0\ &&\ !\[info\ exists\ gcd\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ gcd\ 1\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ x\ \$y\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ y\ \$r\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{!\[info\ exists\ gcd\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{(\$x%\$y)\ ==\ 0\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ gcd\ \$y\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \[lreplace\ \$exprs\ 0\ 0\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ puts\ \"gcd(\$a,\$b)\ =\ \$gcd\"\n\ \ \ \ \ \ \ \ \ puts\ \"-------------\"\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ pn\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 1\]\n\ \ \ \ \ \ \ \ \ set\ pf\ 1\n\ \ \ \ \ \ \ \ \ set\ nn\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 2\]\n\ \ \ \ \ \ \ \ \ set\ nf\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 3\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \[lreplace\ \$exprs\ 0\ 0\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ c\ p\n\ \ \ \ \ \ \ \ \ set\ o\ n\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ puts\ \"\$gcd\ =\ \$pn*\$pf\ -\ \$nn*\$nf\"\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ foreach\ n\ \$exprs\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$c\ eq\ \"p\"\}\ \{set\ c\ n\;\ set\ o\ p\}\ else\ \{set\ c\ p\;\ set\ o\ n\}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 0\ \[lindex\ \$n\ 0\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 1\ \[lindex\ \$n\ 1\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 2\ \[lindex\ \$n\ 2\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 3\ \[lindex\ \$n\ 3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ \$\{c\}n\ \$1\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ \$\{o\}f\ \[expr\ \$\$\{o\}f+(\$\$\{c\}f*\$3)\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ puts\ \"\$gcd\ =\ \$pn*\$pf\ -\ \$nn*\$nf\"\n\ \ \ \ \ \ \ \ \ \}\n\ \}\n======\n\n\[Lars\ H\],\ 2008-09-18:\ Urgh!\ That's\ much\ too\ complicated.\ 11\ lines\ and\ only\ one\ loop\ are\ quite\ sufficient:\n\n======\n\ proc\ xgcd\ \{a\ b\}\ \{\n\ \ \ \ set\ r0\ 1\;\ set\ s0\ 0\n\ \ \ \ set\ r1\ 0\;\ set\ s1\ 1\n\ \ \ \ while\ \{\$b\}\ \{\n\ \ \ \ \ \ \ set\ q\ \[expr\ \{\$a\ /\ \$b\}\]\n\ \ \ \ \ \ \ set\ b\ \[expr\ \{\$a\ -\ \$q*\[set\ a\ \$b\]\}\]\n\ \ \ \ \ \ \ set\ r1\ \[expr\ \{\$r0\ -\ \$q*\[set\ r0\ \$r1\]\}\]\n\ \ \ \ \ \ \ set\ s1\ \[expr\ \{\$s0\ -\ \$q*\[set\ s0\ \$s1\]\}\]\n\ \ \ \ \}\n\ \ \ \ list\ \$a\ \$r0\ \$s0\n\ \}\n======\n\nTo\ demonstrate\ as\ a\ program:\n\n======\n\ proc\ eea\ \{a\ b\}\ \{\n\ \ \ \ foreach\ \{d\ r\ s\}\ \[xgcd\ \$a\ \$b\]\ break\n\ \ \ \ puts\ \"\$d\ =\ \$a*\$r\ +\ \$b*\$s\"\n\ \}\n======\n\nHow\ it\ works\ (beyond\ the\ ordinary\ \[greatest\ common\ denominator\])\ is\ that\n\ \ (\ r0\ \ s0\ )\n\ \ (\ r1\ \ s1\ )\nis\ a\ matrix\ which,\ when\ multiplied\ by\ the\ initial\ (''a'',''b'')\ as\ a\ column\ vector\ on\ the\ right,\ computes\ the\ current\ (''a'',''b''):\n\ \ (\ a\ )\ =\ (\ r0\ \ s0\ )\ (\ a_initial\ )\n\ \ (\ b\ )\ \ \ (\ r1\ \ s1\ )\ (\ b_initial\ )\nSince\ the\ very\ last\ ''a''\ is\ the\ gcd,\ the\ first\ row\ of\ the\ matrix\ gives\ the\ coefficients\ which\ express\ it\ as\ a\ linear\ combination\ of\ the\ arguments.\ The\ three\ lines\ on\ the\ form\n\ \ \ set\ b\ \[expr\ \{\$a\ -\ \$q*\[set\ a\ \$b\]\}\]\nall\ amount\ to\ multiplying\ a\ column\ vector\ on\ the\ left\ by\ a\ matrix\ on\ the\ form\n\ \ (\ 0\ \ 1\ )\n\ \ (\ 1\ -q\ )\n\n----\n***Screenshots***\n\n\[http://farm5.static.flickr.com/4048/4695664749_1e35692ca4.jpg\]\n\n\[gold\]\ added\ pix,\ for\ math\ expression\ table\ script\ above\ \ \ \n\n<<categories>>\ Mathematics|Example regexp2} CALL {my render {Sample Math Programs} These\ sample\ math\ programs\ are\ REALLY\ simple\ examples\ of\ using\ programming\ to\ solve\ math\ problems.\ \ L.\ Prevett,\ a\ Mathematics\ Instructor\ at\ Cochise\ College\ in\ Sierra\ Vista\ Arizona\ (US)\ posted\ a\ request\ on\ the\ seul-edu\ mailing\ list\ \n(\ http://www.seul.org/edu/\ )\ \nasking\ for\ program\ examples.\ \ He\ writes\ \n''I\ think\ the\ reason\ I\ can't\ find\ anything\ like\ this\ is\ that\ it\ is\nTOO\ simple.''\n\nEach\ program\ should\ be\ a\ complete\ sample\ script.\ \ Linux\ users\ should\ be\ able\ to\ save\ the\ code\ to\ a\ file,\ then\ use\ the\ command\ ''tclsh\ filename''\ to\ run\ the\ program.\ \ (Note\ that\ Linux\ users\ might\ have\ to\ type\ ''tclsh<TAB>''\ to\ get\ a\ particular\ version\ of\ tclsh,\ like\ tclsh8.0.)\n\nHere\ is\ a\ list\ of\ simple\ programs\ that\ he\ would\ like\ to\ see.\ \ Can\ you\ help\ write\ one\ of\ them?\ \ I\ think\ the\ goal\ is\ to\ show\ how\ to\ use\ a\ programming\ language\ to\ solve\ a\ problem,\ as\ opposed\ to\ a\ demonstration\ of\ efficient\ algorithms,\ so\ don't\ worry\ too\ much\ about\ tuning\ for\ optimum\ performance.\n\n\ \ \ *\ A\ program\ that\ does\ a\ loop\ and\ prints\ the\ numbers\ from\ 1\ to\ 10\n\ \ \ *\ A\ program\ that\ sums\ the\ numbers\ in\ a\ list\ '''(NEW)''',\ mean,\ square\ mean,\ standard\ deviation\n\ \ \ *\ A\ program\ that\ factors\ a\ number\n\ \ \ *\ A\ program\ that\ generates\ a\ list\ of\ prime\ numbers\ and\ prints\ the\ results\ to\ a\ file\n\ \ \ *\ A\ program\ that\ reads\ a\ list\ of\ numbers\ from\ a\ file\ and\ prints\ their\ factors\n\ \ \ *\ A\ program\ that\ uses\ the\ Euclidean\ Algorithm\ to\ print\ the\ gcd\ of\ two\ numbers\n\ \ \ *\ A\ program\ that\ computes\ a\ factorial\ using\ recursion\n\ \ \ *\ A\ program\ that\ prints\ an\ addition\ table\n\ \ \ *\ A\ program\ that\ prints\ a\ multiplication\ table\n\ \ \ *\ A\ program\ that\ prints\ a\ multiplication\ table\ mod\ n\n\ \ \ *\ A\ program\ that\ prints\ the\ sum\ of\ two\ squares\n\ \ \ *\ A\ program\ that\ inputs\ b,n,\ and\ p\ and\ prints\ b^n\ mod\ p\ for\ n=1\ to\ n-1\n\ \ \ *\ A\ program\ that\ prints\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n.\n\ \ \ *\ A\ program\ that\ computes\ the\ 'osculator'\ for\ a\ prime\ number\ and\ uses\ it\ to\ test\ an\ input\ number\ for\ divisibility\ by\ that\ prime.\n\ \ \ *\ \[Fraction\ math\]\ has\ some\ related\ programs.\n\ \ \ *\ A\ program\ that\ inputs\ a\ number\ and\ a\ counting\ interval\ and\ prints\ the\ Ring\ of\ Josephus.\n\ \ \ *\ A\ program\ that\ prints\ the\ partitions\ of\ a\ number.\n\ \ \ *\ A\ program\ to\ approximate\ pi\ using\ Monte-Carlo\ methods.\n\ \ \ *\ A\ simple\ calculator\ (mathematically\ oriented\ input\ or\ remembered\ functions?)\n\ \ \ *\ A\ program\ that\ computes\ 1000\ digits\ of\ ''e''\ using\ a\ spigot\ algorithm.\n\nAny\ others?\n\n''RWT''\n\n---\n\nFor\ dealing\ with\ complex\ numbers\ see\ \[Complex\ math\ made\ simple\].\n\n---\n\nAfter\ conferring\ with\ RWT\ I\ put\ these\ programs\ along\ with\nmost\ of\ the\ comments\ in\ the\ K-12\ Mathematics\ Teachers\ Guide\ for\ Linux\n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/outline.html\]\nbeing\ developed\ for\ SEUL.\ The\ programs\ are\ in\ the\ \"Programming\ Mathematics\"\ \ \[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/programming_math.html\]\ \nsection\ under\ \"tcl/tk\"\ \n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/tcl/tcltk_math.html\].\ \nThere\ is\ also\ a\ link\ to\ \"Suggested\ Programming\ Problems\ for\ Mathematics\"\ \n\[http://math.cochise.cc.az.us/math/K12/teacher_guide/programming/suggested_progs.html\]\ on\ that\ page.\n\nThanks\ to\ all\ the\ members\ of\ the\ comp.lang.tcl\ newsgroup\ and\ the\ Tcler's\nWiki\ who\ have\ contributed\ programs.\ If\ I\ didn't\ get\ some\ of\ the\ncredits\ right,\ or\ if\ I\ missed\ a\ credit,\ or\ if\ you\ have\ any\ comments\ or\ other\ issues,\ please\ feel\ free\ to\ contact\ me.\n\nL.\ Prevett,\nMathematics\ Instructor\nCochise\ College,\ Sierra\ Vista,\ AZ,\ US\[email protected],\n11-2-00\n\n----\n\[LV\]\ I\ was\ wondering\ today\ whether\ the\ original\ requestor\ was\ interested\ in\ examples\ that\ demonstrate\ how\ one\ can\ make\ use\ of\ fancy\ tcl\ tricks\ to\ get\ the\ fastest\ answers,\ examples\ that\ demonstrate\ the\ underlying\ mathematical\ concepts,\ or\ just\ examples\ of\ any\ code\ that\ provides\ the\ reader\ with\ answers\ (and\ who\ cares\ how\ or\ why\ the\ program\ works)?\ \n\n----\n\n**A\ program\ that\ does\ a\ loop\ and\ prints\ the\ numbers\ from\ 1\ to\ 10**\n======\n\ #\ A\ program\ that\ does\ a\ loop\ and\ prints\ \n\ #\ the\ numbers\ from\ 1\ to\ 10\n\ #\n\ set\ n\ 10\ \n\ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ puts\ \$i\n\ \}\n======\nor\ alternatively\n======\n\ #\ A\ program\ that\ does\ a\ loop\ and\ prints\ \n\ #\ the\ numbers\ from\ 1\ to\ 10\n\ #\n\ set\ i\ 1\n\ while\ \{\$i\ <=\ 10\}\ \{\n\ \ \ \ \ puts\ \$i\n\ \ \ \ \ incr\ i\n\ \}\n======\nThese\ are\ just\ like\ in\ C.\ Another,\ Tcl-only\ alternative\ for\ short\ lists:\n======\n\ foreach\ i\ \{1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9\ 10\}\ \{puts\ \$i\}\ \;#RS\n======\n----\n\n**A\ program\ that\ sums\ the\ numbers\ in\ a\ list**\n======\n\ proc\ lsum\ L\ \{expr\ \[join\ \$L\ +\]+0\}\n======\nThis\ brief\ little\ beauty\ does\ no\ I/O\ (which\ should\ be\ separated\ anyway).\ In\ an\ interactive\ tclsh,\ the\ last\ result\ is\ automatically\ output.\ So\ you\ can\ test\ it\ with\n======\n\ lsum\ \{1\ 2\ 3\ 4.5\}\n\ %\ 10.5\n======\nThis\ very\ Tcl'ish\ style\ concatenates\ the\ elements\ of\ L\ with\ a\ plus\ sign,\ which\ then\ looks\ like\ ''1+2+3+4.5'',\nappends\ +0\ to\ the\ end\ (just\ in\ case\ the\ list\ was\ empty)\ and\ has\ the\ resulting\ string\ evaluated\ by\ expr\ (and\ as\ DKF\ measured,\ this\ is\ pretty\ efficient\ -\ see\ \[Lambda\ in\ Tcl\]).\ As\ the\ ''expr''\ is\ the\ (first,\ only,\ and)\ last\ command\ in\ the\ proc,\ an\ explicit\ ''return''\ is\ not\ needed.\ Again\ for\ conciseness,\ a\ one-element\ arg\ list\ for\ proc\ need\ not\ be\ braced.\nWhile\ we're\ at\ it,\ the\ arithmetic\ average\ can\ be\ done\ with\n======\n\ proc\ lavg\ L\ \{expr\ (\[join\ \$L\ +\])/\[llength\ \$L\].\}\n======\nWe\ don't\ have\ to\ provide\ for\ empty\ lists\ here,\ since\ we\ can't\ divide\ by\ a\ length\ of\ 0...\ See\ \[Steps\ towards\ functional\ programming\]\ on\ how\ to\ use\ this\ in\ powerful\ one-liners.\ -\ As\ I\ just\ needed\ them,\ here's\ '''square\ mean'''\ and\ '''standard\ deviation'''\ also:\n======\n\ proc\ mean2\ list\ \{\n\ set\ sum\ 0\n\ foreach\ i\ \$list\ \{set\ sum\ \[expr\ \{\$sum+\$i*\$i\}\]\}\n\ expr\ \{double(\$sum)/\[llength\ \$list\]\}\n\ \}\n\ proc\ stddev\ list\ \{\n\ set\ m\ \[lavg\ \$list\]\n\ expr\ \{sqrt(\[mean2\ \$list\]-\$m*\$m)\}\n\ \}\ \;#\ RS\n======\n----\n#\ mad\ -\ I\ tried\ the\ above\ lsum\ implementation\ with\ large\ lists\ having\ elements\ in\ excess\ of\ 30,000.\ It\ gets\ very\ slow\ and\ results\ in\ segmentation\ fault.\ So,\ take\ a\ judgement\ on\ based\ on\ your\ use\ case.\n----\n**A\ program\ that\ factors\ a\ number**\n======\n\ #\ A\ program\ that\ factors\ a\ number\n\ puts\ -nonewline\ \"Enter\ number\ to\ factor:\ \ \"\n\ set\ num\ \[gets\ stdin\]\n\n\ for\ \{set\ i\ \$num\}\ \{\$i>0\}\ \{incr\ i\ -1\}\ \{\n\ \ \ \ \ if\ \{\ \$num\ %\ \$i\ ==\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \$i\n\ \ \ \ \ \}\n\ \}\n======\nMS:\ I\ understand\ \"factor\ a\ number\"\ as\ \"producing\ the\ list\ of\ prime\ \nfactors\"\;\ the\ algorithm\ above\ produces\ instead\ a\ list\ of\ all\ integer\ndivisors\ (after\ cleaning\ up\ the\ syntax\ ...)\n\nSo,\ I\ propose\ instead:\n======\n\ #\ A\ program\ that\ factors\ a\ number\n\ proc\ factor_num\ \{num\}\ \{\n\ \ \ \ \ for\ \{set\ i\ 2\}\ \{\$i\ <=\ \$num\}\ \{\}\ \{\n\ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$num\ %\ \$i\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ lappend\ factors\ \$i\n\ \ \ \ \ \ \ \ \ \ \ set\ num\ \[expr\ \{\$num\ /\ \$i\}\]\n\ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ incr\ i\n\ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$factors\n\ \}\n\ \ \ \ \ \n\ proc\ factor_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Enter\ number\ to\ factor:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ num\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\ \[expr\ \{int(abs(\$num))\}\]\ !=\ \$num\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \"Sorry:\ only\ non-negative\ integers\ allowed\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ set\ factors\ \[factor_num\ \$num\]\n\ \ \ \ \ \ \ \ \ puts\ \"\$num\ =\ \[join\ \$factors\ \{\ *\ \}\]\"\n\ \ \ \ \ \}\n\ \}\n\n\ factor_run\n\ #\ end\ of\ program\n======\nMore\ efficient\ for\ large\ numbers\ would\ be\n======\n\ proc\ factor_num\ \{num\}\ \{\n\ \ \ \ \ for\ \{set\ i\ 2\}\ \{\$i\ <=\ \$num\}\ \{\}\ \{\n\ \ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$num\ %\ \$i\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ factors\ \$i\n\ \ \ \ \ \ \ \ \ \ \ \ \ set\ num\ \[expr\ \{\$num\ /\ \$i\}\]\n\ \ \ \ \ \ \ \ \ \}\ elseif\ \{\[expr\ \$i*\$i\ >\ \$num\]\}\ \{\n\ \ \ \ \ \ lappend\ factors\ \$num\n\ \ \ \ \ \ break\n\ \ \}\ elseif\ \{\$i\ !=\ 2\}\ \{\ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ incr\ i\ 2\n\ \ \}\ else\ \{\n\ \ \ \ \ \ set\ i\ 3\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$factors\n\ \}\n======\n----\n\n''''italic\ text''''**A\ program\ that\ generates\ a\ list\ of\ prime\ numbers\ and\ prints\ the\ results\ to\ a\ file**\n======\n\ proc\ all_primes\ \{max\}\ \{\n\ \ \ \ \ set\ primes\ \[list\ 2\]\n\ \ \ \ \ for\ \{set\ test\ 3\}\ \{\$test\ <=\ \$max\}\ \{incr\ test\ 2\}\ \{\n\ \ \ \ \ \ \ \ \ set\ maxTest\ \[expr\ \{int(sqrt(\$test))\}\]\n\ \ \ \ \ \ \ \ \ foreach\ prime\ \$primes\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$prime\ \ >\ \$maxTest\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ primes\ \$test\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break\n\ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{!\[expr\ \{\$test\ %\ \$prime\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break\n\ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$primes\n\ \}\n\ \ \ \ \ \ \n\ proc\ all_primes_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Compute\ all\ primes\ below:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ max\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\$max\ <\ 2\}\ \{\n\ \ \ \ \ \ \ \ \ set\ max\ 2\n\ \ \ \ \ \}\n\ \ \ \ \ set\ primes\ \[all_primes\ \$max\]\n\n\ \ \ \ \ set\ f\ \[open\ \"primes_below_\$max\"\ w\]\n\ \ \ \ \ puts\ \$f\ \$primes\n\ \ \ \ \ close\ \$f\n\ \}\n\n\ all_primes_run\n\ #\ end\ of\ program\n======\n----\n\n**A\ program\ that\ reads\ a\ list\ of\ numbers\ from\ a\ file\ and\ prints\ their\ factors.**\n======\n\ #\ Start\ by\ including\ one\ of\ the\ factor_num\ procedures\n\ #\ defined\ above,\ then\ just\ read\ a\ file\ print\ the\ factors.\n\ set\ f\ \[open\ \"test.dat\"\ r\]\n\ while\ \{\ !\ \[eof\ \$f\]\ \}\ \{\n\ \ \ \ scan\ \[gets\ \$f\]\ %i\ number\n\ \ \ \ set\ factors\ \[factor_num\ \$number\]\n\ \ \ \ puts\ \"\$number:\ \ \$factors\"\n\ \}\n\ close\ \$f\n======\n\n----\n\n**A\ program\ that\ uses\ the\ Euclidean\ Algorithm\ to\ print\ the\ GCD\ (greatest\ common\ divisor)\ of\ two\ numbers**\n======\n\ proc\ gcd\ \{num1\ num2\}\ \{\n\ \ \ \ while\ \{\[set\ tmp\ \[expr\ \{\$num1%\$num2\}\]\]\}\ \{\n\ \ \ \ \ \ \ set\ num1\ \$num2\n\ \ \ \ \ \ \ set\ num2\ \$tmp\n\ \ \ \ \}\n\ \ \ \ return\ \$num2\n\ \}\n======\n\n----\n\n**A\ program\ that\ computes\ a\ factorial\ using\ recursion***\n======\n\ proc\ fact\ \{num\}\ \{\n\ \ \ \ \ if\ \{\$num\}\ \{\n\ \ \ \ \ \ \ \ \ return\ \[expr\ \{\$num\ *\ \[fact\ \[incr\ num\ -1\]\]\}\]\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ return\ 1\n\ \ \ \ \ \}\n\ \}\n\n\ proc\ fact_run\ \{\}\ \{\n\ \ \ \ \ puts\ -nonewline\ \"Compute\ the\ factorial\ of:\ \ \"\n\ \ \ \ \ flush\ stdout\n\ \ \ \ \ set\ num\ \[gets\ stdin\]\n\ \ \ \ \ if\ \{\ \[expr\ \{int(abs(\$num))\}\]\ !=\ \$num\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ \"Sorry:\ only\ non-negative\ integers\ allowed\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ puts\ \[fact\ \$num\]\n\ \ \ \ \ \}\n\ \}\n\n\ fact_run\n======\n''DKF''\ -\ It\ is\ more\ efficient\ to\ use\ iteration:\n======\n\ proc\ fact\ \{n\}\ \{\n\ \ \ \ set\ f\ 1\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ f\ \[expr\ \{\$f\ *\ \$i\}\]\n\ \ \ \ \}\n\ \ \ \ return\ \$f\n\ \}\n======\n----\n\n**A\ program\ that\ prints\ some\ mathematical\ operation\ tables!**\n======\n\ proc\ table\ \{\ op\ N\ \}\ \{\n\ \ \ \ \ \ set\ table\ \"\$op\ table\\n\"\n\ \ \ \ \ \ for\ \{\ set\ i\ 1\ \}\ \{\ \$i\ <=\ \$N\ \}\ \ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ lappend\ line\ \$i\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ set\ i\ 0\n\ \ \ \ \ \ set\ magic\ \{\}\n\ \ \ \ \ \ switch\ -exact\ --\ \$op\ \{\n\ \ \ \ \ \ \ \ \ +\ \{\ set\ i\ -2\ \}\n\ \ \ \ \ \ \ \ \ /\ \{\ set\ magic\ .0\ \}\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ while\ \{\ \[\ incr\ i\ \]\ <=\ \$N\ \}\ \{\n\ \ \ \ \ \ \ \ \ foreach\ item\ \$line\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ append\ table\ \"\ \[\ expr\ \$item\$op\$i\$magic\ \]\"\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ append\ table\ \"\\n\"\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ set\ table\n\ \}\n\n\ foreach\ op\ \{\ *\ +\ /\ -\ ==\ !=\ >\ >=\ <\ <=\ \}\ \{\n\ \ \ \ puts\ \[\ table\ \$op\ 10\ \]\n\ \}\n======\n''-PSE''\n----\n\n**A\ program\ displaying\ any\ expression\ table\ (wish\ version)**\n======\n\ #\ a\ program\ displaying\ any\ expression\ table\ (FP)\n\n\ set\ first\ 0\n\ set\ last\ 9\n\ set\ expr\ \{\$a+\$b\}\n\ #\ can\ be\ replaced\ interactively\ by\ \{\$a*\$b\},\ \{(\$a*\$b)%7)\},\ ...\n\n\ set\ LP\ lp\ \ \;#\ machine\ dependent\ print\ command\n\ \n\ proc\ showTable\ \{first\ last\ oper\}\ \{\n\ \ \ \ \ set\ terms\ \[list\]\n\ \ \ \ \ for\ \{set\ i\ \$first\}\ \{\$i\ <=\ \$last\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ \ \ lappend\ terms\ \$i\n\ \ \ \ \ \}\n\ \ \ \ \ set\ n\ \[llength\ \$terms\]\n\ \ \ \ \ \n\ \ \ \ \ set\ hdx\ 12\n\ \ \ \ \ set\ hdy\ 11\n\ \ \ \ \ set\ bx\ 20\n\ \ \ \ \ set\ by\ 20\n\ \ \ \ \ \n\ \ \ \ \ .c\ delete\ all\n\ \ \ \ \ .c\ configure\ -width\ \[expr\ \{2*(\$n+1)*\$hdx\ +\ 2*\$bx\}\]\ -height\ \[expr\ \{(2*\$n+4)*\$hdy\ +\ 2*\$by\}\]\n\ \ \ \ \ set\ ia\ 0\n\ \ \ \ \ foreach\ a\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ incr\ ia\n\ \ \ \ \ \ \ \ \ set\ ib\ 0\n\ \ \ \ \ \ \ \ \ foreach\ b\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ incr\ ib\n\ \ \ \ \ \ \ \ \ \ \ \ \ set\ r\ \[expr\ \$oper\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{(2*\$ib+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{(2*\$ia+1)*\$hdy\ +\ \$by\}\]\ -text\ \$r\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \ \ \ \ .c\ create\ text\ \[expr\ \{(\$n+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{(2*\$n+4)*\$hdy\ +\ \$by\}\]\ -text\ \$oper\n\ \ \ \ \ \n\ \ \ \ \ set\ ia\ 0\n\ \ \ \ \ foreach\ a\ \$terms\ \{\n\ \ \ \ \ \ \ \ \ incr\ ia\n\ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{(2*\$ia+1)*\$hdx\ +\ \$bx\}\]\ \[expr\ \{\$by\ +\ \$hdy\}\]\ -text\ \$a\n\ \ \ \ \ \ \ \ \ .c\ create\ text\ \[expr\ \{\$bx\ +\ \$hdx\}\]\ \[expr\ \{(2*\$ia+1)*\$hdy\ +\ \$by\}\]\ -text\ \$a\n\ \ \ \ \ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ (2*\$ia+2)*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n\ +\ 2)*\$hdx\}\]\ \[expr\ \{\$by\ +\ (2*\$ia+2)*\$hdy\}\]\n\ \ \ \ \ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$ia+2)*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$ia+2)*\$hdx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\n\ \ \ \ \ \}\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdx\}\]\ \[expr\ \{\$by\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdx\}\]\ \[expr\ \{\$by\ +\ 2*\$hdy\}\]\ -width\ 2\n\ \ \ \ \ .c\ create\ line\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$by\}\]\\\n\ \ \ \ \ \ \ \ \ \ \ \ \ \[expr\ \{\$bx\ +\ 2*\$hdx\}\]\ \[expr\ \{\$bx\ +\ (2*\$n+2)*\$hdy\}\]\ -width\ 2\n\ \}\n\ \n\ canvas\ .c\n\ pack\ .c\n\ \n\ frame\ .f\n\ label\ .f.l1\ -text\ from:\n\ label\ .f.l2\ -text\ to:\n\ entry\ .f.e1\ -textvariable\ first\ -width\ 2\n\ entry\ .f.e2\ -textvariable\ last\ -width\ 2\n\ label\ .f.l3\ -text\ \{expr:\}\n\ entry\ .f.e3\ -textvariable\ expr\n\ pack\ .f.l1\ .f.e1\ .f.l2\ .f.e2\ -side\ left\n\ pack\ .f.e3\ .f.l3\ -side\ right\n\ pack\ .f\ -fill\ x\n\ \n\ button\ .bp\ -text\ print\ -command\ \{exec\ \$LP\ <<\ \[.c\ postscript\]\}\ \n\ button\ .bq\ -text\ exit\ -command\ exit\ \n\ pack\ .bq\ .bp\ -side\ left\n\n\ button\ .b\ -text\ show\ -command\ \{showTable\ \$first\ \$last\ \$expr\}\n\ pack\ .b\ -side\ right\ -expand\ y\ -fill\ x\n\ .b\ invoke\n\n\ #\ end\ of\ program\n======\n----\n\n**A\ program\ that\ prints\ a\ multiplication\ table**\n======\n\ #\ A\ program\ to\ print\ a\ multiplication\ table\ to\ the\ screen\n\n\ proc\ times_table\ \{\ x\ \}\ \{\n\ \ \ \ puts\ \"Multiplication\ table\ for\ \$x.\"\n\ \ \ \ for\ \{set\ i\ 1\ \}\ \{\ \$i\ <\ 10\}\ \{incr\ i\ \}\ \{\n\ \ \ \ \ \ \ set\ answer\ \[expr\ \$x\ *\ \$i\]\n\ \ \ \ \ \ \ puts\ \"\$x\ times\ \$i\ =\ \ \$answer\"\n\ \ \ \ \}\n\ \}\n\n\ proc\ run_table\ \{\ \}\ \{\n\ \ \ \ puts\ -nonewline\ \"Enter\ a\ number:\ \ \"\n\ \ \ \ flush\ stdout\n\ \ \ \ set\ x\ \[gets\ stdin\]\n\ \ \ \ times_table\ \$x\n\ \}\n\n\ run_table\n\ #end\ of\ program\n======\n----\n\n**A\ program\ that\ prints\ a\ multiplication\ table\ mod\ n**\n\nI\ noticed\ that\ the\ 'any\ expression'\ table\ script\ above\ \nwill\ print\ a\ multiplication\ (or\ addition,\ etc)\ table\ mod\ ''n''.\n\nFor\ example,\ \n\n\ \ from:\ 1\ to:\ 4\ expr:\ (\$a\ *\ \$b)\ %\ 5\n\nprints\ the\ multiplication\ table\ mod\ 5.\n\nBy\ the\ way,\ these\ scripts\ are\ really\ great!\n\n''lp''\n\n----\n\n**A\ program\ that\ prints\ the\ sum\ of\ two\ squares**\n======\n\ proc\ sum_of_two_squares\ \{\ x\ y\ \}\ \{\n\ \ \ \ set\ answer\ \[expr\ \$x*\$x\ +\ \$y*\$y\]\n\ \ \ \ puts\ \$answer\n\ \}\n======\n''so''\n----\n\n**A\ program\ that\ inputs\ b,n,\ and\ p\ and\ prints\ b^n\ mod\ p\ for\ n=1\ to\ n-1**\n\n\[KBK\]\ (4\ Oct\ 2000):\n======\n\ proc\ modular_exponent_table\ \{\ b\ p\ \}\ \{\n\ \ \ \ set\ value\ 1\n\ \ \ \ for\ \{set\ n\ 1\}\ \{\$n\ <\ \$p\}\ \{incr\ n\}\ \{\n\ \ \ \ \ \ \ set\ value\ \[expr\ \{(\$value\ *\ \$b)\ %\ \$p\}\]\n\ \ \ \ \ \ \ puts\ \"(\ \$b\ **\ \$n\ )\ mod\ \$p\ ==\ \$value\"\n\ \ \ \ \}\n\ \}\;#\ edited\ for\ clarity\ and\ style\ by\ DKF\n\n\ puts\ -nonewline\ stdout\ \"Enter\ base\ b:\ \"\n\ flush\ stdout\n\ gets\ stdin\ b\n\ puts\ -nonewline\ stdout\ \"Enter\ modulus\ p:\ \"\n\ flush\ stdout\n\ gets\ stdin\ p\n\n\ modular_exponent_table\ \$b\ \$p\n======\nRunning\ the\ above\ gives:\n======\n\ %\ tclsh83\ prime_power.tcl\n\ Enter\ base\ b:\ 3\n\ Enter\ modulus\ p:\ 17\n\ (\ 3\ **\ 1\ )\ mod\ 17\ ==\ 3\n\ (\ 3\ **\ 2\ )\ mod\ 17\ ==\ 9\n\ (\ 3\ **\ 3\ )\ mod\ 17\ ==\ 10\n\ (\ 3\ **\ 4\ )\ mod\ 17\ ==\ 13\n\ (\ 3\ **\ 5\ )\ mod\ 17\ ==\ 5\n\ (\ 3\ **\ 6\ )\ mod\ 17\ ==\ 15\n\ (\ 3\ **\ 7\ )\ mod\ 17\ ==\ 11\n\ (\ 3\ **\ 8\ )\ mod\ 17\ ==\ 16\n\ (\ 3\ **\ 9\ )\ mod\ 17\ ==\ 14\n\ (\ 3\ **\ 10\ )\ mod\ 17\ ==\ 8\n\ (\ 3\ **\ 11\ )\ mod\ 17\ ==\ 7\n\ (\ 3\ **\ 12\ )\ mod\ 17\ ==\ 4\n\ (\ 3\ **\ 13\ )\ mod\ 17\ ==\ 12\n\ (\ 3\ **\ 14\ )\ mod\ 17\ ==\ 2\n\ (\ 3\ **\ 15\ )\ mod\ 17\ ==\ 6\n\ (\ 3\ **\ 16\ )\ mod\ 17\ ==\ 1\n======\n\[KBK\]\ (10\ Oct\ 2000):\ I'm\ not\ sure\ that\ I\ like\ the\ change\ that\n''DKF''\ made.\ \ Let's\ follow\ this\ up\ a\ little\ bit\ on\ \[Tcl\ style\ discussion\].\n----\n\n**A\ program\ that\ computes\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n**\n======\n\ #\ A\ program\ to\ compute\ the\ sum\ of\ the\ numbers\ from\ 1\ to\ n\n\n\ proc\ sumto\ \{n\}\ \{\n\ \ \ \ set\ sum\ 0\n\ \ \ \ set\ i\ 1\n\ \ \ \ while\ \{\$i\ <=\ \$n\}\ \{\n\ \ \ \ \ \ \ set\ sum\ \[expr\ \$sum\ +\ \$i\]\n\ \ \ \ \ \ \ incr\ i\n\ \ \ \ \}\n\ \ \ \ puts\ \"The\ sum\ of\ the\ numbers\ from\ 1\ to\ \$n\ is\ \ \$sum\ \"\n\ \}\n\n\ puts\ -nonewline\ stdout\ \"Enter\ a\ number:\ \"\n\ flush\ stdout\n\ gets\ stdin\ n\n\ sumto\ \$n\n\n\ #\ end\ of\ program\n\ #\ I'm\ no\ programmer,\ but\ I\ wanted\ to\ see\ if\ I\ could\ use\ the\ examples\n\ #\ on\ this\ page\ to\ write\ this\ program.\ I\ think\ it\ works\ ...\n======\n''lp''\n\n''Minor\ style\ note''\ -\ It\ is\ better\ to\ use\ the\ \[\[for\]\]\ command\ for\ the\ style\ of\ loop\ you\ were\ doing\ above\ as\ it\ is\ easier\ to\ understand\ the\ purpose\ of\ the\ loop\ when\ you\ come\ back\ to\ look\ at\ it\ later\;\ code\ has\ this\ way\ of\ lasting\ far\ longer\ than\ you\ intended...\ \ '''DKF'''\n\nRS\ --\ Also,\ a\ proc\ should\ return\ the\ result\ rather\ than\ puts'ing\ it.\ So\ here's\ my\ version:\n======\n\ proc\ sumto\ n\ \{\n\ \ \ \ \ set\ sum\ 0\n\ \ \ \ \ for\ \{\ set\ i\ 1\ \}\ \{\ \$i\ <=\ \$n\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ \ incr\ sum\ \$i\n\ \ \ \ \ \}\n\ \ \ \ \ return\ \$sum\n\ \}\n======\n\[KBK\]\ --\ I\ edited\ this\ (18\ October\ 2000)\ to\ spare\ Donal's\ tender\ sensibilities\ (see\ \[Tcl\ style\ discussion\]).\n\nJCE\ -\ so\ why\ not\ just\ this:\n======\n\ proc\ sumto\ n\ \{\n\ \ \ \ \ expr\ \$n\ *\ (\$n\ +\ 1)\ /\ 2\n\ \}\n======\n----\n\n**A\ program\ that\ computes\ the\ osculator\ for\ a\ prime\ number,\ and\ uses\ it\ to\ test\ an\ input\ number\ for\ divisibility\ by\ that\ prime**\n======\n\ #\ I\ thought\ the\ group\ might\ be\ interested\ in\ this\ problem.\n\ #\ This\ would\ be\ a\ more\ challenging\ program\ for\ students\ to\n\ #\ program\ because\ they\ have\ to\ access\ individual\ digits\ in\n\ #\ numbers\ to\ form\ new\ numbers.\n\n\ #\ Numbers\ that\ end\ in\ even\ numbers\ or\ 5\ are\ easy\ to\ factor.\n\ #\ Numbers\ that\ end\ in\ 1,3,7,9\ are\ generally\ more\ difficult.\n\n\ #\ Divisibility\ tests\ exist\ for\ numbers\ that\ end\ in\n\ #\ 1,3,7,\ or\ 9,\ but\ they\ use\ a\ special\ 'osculator'.\n\ #\ For\ example,\ the\ osculator\ for\ 7\ is\ 5.\ To\ test\ 343\ for\n\ #\ divisibility\ by\ 7,\ here's\ what\ you\ do:\n\ #\ \ \ multiply\ the\ rightmost\ digit\ by\ the\ osculator,\ 5:\ \ 3x5\ =\ 15\n\ #\ \ \ add\ the\ result\ to\ all\ the\ digits\ to\ the\ left\ of\ 3:\ 34\ +\ 15\ =\ 49\n\ #\ since\ 49\ is\ divisible\ by\ 7,\ 343\ will\ be\ divisible\ by\ 7.\ \n\n\ #\ To\ compute\ the\ osculator\ for\ any\ number,\ first\ multiply\n\ #\ by\ (the\ smallest)\ number\ that\ gives\ a\ product\ whose\ rightmost\n\ #\ digit\ is\ '9'.\ Drop\ the\ '9'\ and\ add\ 1\ to\ all\ the\ remaining\ digits.\n\ #\ The\ osculator\ for\ 71\ is\ 64:\ 71\ x\ 9\ =\ 639,\ 63\ +\ 1\ =\ 64.\n\ #\ The\ osculator\ for\ 23\ is\ \ 7:\ 23\ x\ 3\ =\ \ 69,\ \ 6\ +\ 1\ =\ 7.\n\ #\ The\ osculator\ for\ 17\ is\ 12:\ 17\ x\ 7\ =\ 119,\ 11\ +\ 1\ =\ 12.\n\ #\ The\ osculator\ for\ 19\ is\ \ 2:\ 19\ x\ 1\ =\ \ 19,\ \ 1\ +\ 1\ =\ 2.\n\n\ #\ Another\ example.\ Is\ 3534\ divisible\ by\ 19?\n\ #\ The\ osculator\ for\ 19\ is\ 2.\n\ #\ 4\ x\ 2\ =\ 8,\ 353\ +\ 8\ =\ 361\n\ #\ 1\ x\ 2\ =\ 2,\ \ 36\ +\ 2\ =\ \ 38\n\ #\ Since\ 38\ is\ divisible\ by\ 19,\ 3534\ is\ divisible\ by\ 19.\n\n\ #\ Incidentally,\ if\ the\ sum\ of\ the\ digits\ of\ a\ number\ sum\ to\ '3'\n\ #\ then\ the\ number\ itself\ is\ divisible\ by\ 3.\ This\ is\ because\ the\n\ #\ osculator\ for\ 3\ is\ '1':\ 3\ x\ 3\ =\ 09,\ 0\ +\ 1\ =\ 1.\n======\n''lp''\n\ \n\nYou\ do\ not\ really\ need\ to\ access\ ''individual''\ digits:\ just\ the\ last\ one,\ and\ the\ rest.\ This\ can\ be\ done\ simply\ by\ dividing\ by\ 10\ (modular\ or\ integer\ division).\ For\ instance,\ to\ compute\ the\ osculator\ of\ any\ number,\ just\ do:\n======\n\ proc\ osc\ \{base\}\ \{\n\ \ \ \ for\ \{set\ i\ 1\}\ \{1\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ tmp\ \[expr\ \{\$base\ *\ \$i\}\]\n\ \ \ \ \ \ \ if\ \[expr\ \{(\$tmp\ %\ 10)\ ==\ 9\}\]\ \{\n\ \ \ \ \ \ \ \ \ \ \ return\ \[expr\ \{(\$tmp\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \ \ \ \}\n\ \ \ \ \}\n\ \}\n======\nA\ safer\ version\ is\n======\n\ array\ set\ mult\ \[list\ 1\ 9\ 3\ 3\ 7\ 7\ 9\ 1\]\n\ proc\ osc\ \{base\}\ \{\n\ \ \ \ \ global\ mult\n\ \ \ \ \ set\ last\ \[expr\ \{\$base\ %\ 10\}\]\n\ \ \ \ \ if\ \{\[catch\ \{set\ coeff\ \$mult(\$last)\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ return\ \"Only\ valid\ for\ numbers\ ending\ in\ 1,\ 3,\ 7\ or\ 9\ ...\"\n\ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ return\ \[expr\ \{((\$base\ *\ \$coeff)\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \ \}\n\ \}\n======\n''MS''\n\n''DKF''\ -\ We\ can\ leverage\ Tcl's\ string/number\ duality\ capabilities\ to\ help\ here.\ \ Note,\ there's\ not\ many\ other\ languages\ where\ you'd\ solve\ the\ problem\ like\ this...\n======\n\ proc\ osc\ base\ \{\n\ \ \ \ if\ \{\[regexp\ \{\[1379\]\$\}\ \$base\ digit\]\}\ \{\n\ \ \ \ \ \ \ set\ coeff\ \[lindex\ \{?\ 9\ ?\ 3\ ?\ ?\ ?\ 7\ ?\ 1\}\ \$digit\]\n\ \ \ \ \ \ \ return\ \[expr\ \{(\$base\ *\ \$coeff\ /\ 10)\ +\ 1\}\]\n\ \ \ \ \}\n\ \ \ \ return\ -code\ error\ \"base\ must\ end\ in\ 1,\ 3,\ 7\ or\ 9\ ...\"\n\ \}\n======\n\n''lp''\ -\ So,\ here's\ a\ mangled\ attempt\ at\ a\ divisbility\ test\ using\ one\ of\ the\ three\ 'osc'\ procs\ above.\n======\n\ #\ perform\ one\ osculation\ of\ n\ by\ m\n\ proc\ oscit\ \{n\ m\}\ \{\n\ \ \ \ \ set\ adder\ \[expr\ \{\$m*(\$n\ %\ 10)\}\]\n\ \ \ \ \ set\ left_digits\ \[expr\ \{int(\$n\ /\ 10)\}\]\n\ \ \ \ \ return\ \[expr\ \{\$left_digits\ +\ \$adder\}\]\n\ \}\n\n\ #\ Test\ the\ divisibilty\ of\ num\ by\ prime.\n\ #\ use\ a\ maximum\ of\ 9\ digits\ for\ the\ number\n\ #\ \ \ \ \ \ \ 123456789\n\ set\ num\ 211111043\n\ set\ prime\ 23\n\n\ #\ get\ the\ osculator\ for\ the\ prime\n\ set\ osc_p\ \[osc\ \[expr\ \$prime\]\]\n\n\ #\ get\ the\ number\ of\ digits\ in\ the\ number\n\ #\ will\ osculate\ the\ number\ this\ many\ times\n\ set\ n_digits\ \[string\ length\ \$num\]\n\n\ #\ compute\ and\ print\ osculations\n\ set\ x\ \$num\n\ for\ \{set\ j\ 1\ \}\ \{\ \$j\ <\ \$n_digits\}\ \{incr\ j\ \}\ \{\n\ \ \ \ puts\ -nonewline\ \"\$x,\ \"\n\ #\ proper\ way\ to\ call\ oscit\ with\ 2\ vars?\n\ \ \ \ set\ x\ \[oscit\ \[expr\ \$x\]\ \[expr\ \$osc_p\]\]\n\ \}\n\ puts\ \"\$x\ ...\"\n\n\ #\ print\ the\ results\n\ puts\ \"\$n_digits\ osculations\ of\ \$num\ by\ osculator\ \$osc_p\ for\ prime\ \$prime.\"\n\ if\ \[expr\ !(\$x\ %\ \$prime)\]\ \{\n\ \ \ puts\ \"Since\ \$x\ is\ divisible\ by\ \$prime,\ \$num\ is\ divisible\ by\ \$prime.\"\n\ \}\ else\ \{\n\ \ \ puts\ \"Since\ \$x\ is\ not\ divisible\ by\ \$prime,\ \$num\ is\ not\ divisible\ by\ \$prime.\"\n\n\ #\ A\ note\ for\ the\ 'else'\ case\ ...\n\ #\ The\ following\ is\ an\ example\ of\ the\ kinds\ of\ patterns\ that\ can\ be\n\ #\ noticed\ in\ mathematics\ with\ the\ use\ of\ a\ computer.\ Such\ patterns\ \ \ \n\ #\ often\ lead\ to\ new\ insights,\ new\ proofs,\ new\ theorems,\ etc.\n\ \ \ puts\ \"Conjecture:\ continued\ osculation\ produces\ a\ list\ of\ numbers\\\ \ \ \n\ \ \ with\ period\ \$prime\ -\ 1\ =\ \[expr\ \$prime\ -\ 1\]:\"\n\ \ \ \ \ \ for\ \{set\ j\ 1\ \}\ \{\ \$j\ <\ \$prime\ \}\ \{incr\ j\ \}\ \{\n\ \ \ \ \ \ \ \ \ puts\ -nonewline\ \"\$x,\ \"\n\ \ \ \ \ \ \ \ \ set\ x\ \[oscit\ \[expr\ \$x\]\ \[expr\ \$osc_p\]\]\n\ \ \ \ \ \ \}\n\ \ \ \ \ \ puts\ \"\$x\ ...\"\n\ \ \ \ \ \ puts\ \"Conjecture:\ It\ appears\ that\ each\ number\ in\ the\ first\ half\\\ \n\ \ \ \ \ \ added\ to\ its\ conjugate\ in\ the\ second\ half\ is\ divisible\ by\ \$prime.\"\n\ \ \ \}\n\ #\ end\ program\n======\n''lp''\n\n----\n\n**A\ program\ that\ prints\ the\ Ring\ of\ Josephus**\n======\n\ #\ For\ the\ Ring\ of\ Josephus,\ you\ start\ with\ a\ list\ of\ elements\n\ #\ from\ 1\ to\ n,\ and\ a\ counting\ interval,\ k.\n\ #\ You\ make\ one\ pass\ through\ the\ list\ selecting\ each\ kth\ element\n\ #\ in\ the\ list.\ You\ 'delete'\ each\ element\ as\ it\ is\ selected.\n\ #\ You\ continue\ to\ select\ and\ delete\ each\ kth\ element\ until\n\ #\ no\ more\ elements\ remain.\ The\ permutation\ generated\n\ #\ describes\ the\ order\ in\ which\ each\ number\ has\ been\ chosen.\n\n\ #\ First\ selection\ for\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ _\ \ \ _\ \ \ _\ \ \ 1\ \ \ _\ \ \ _\ \ \ _\ \ <--\ order\ chosen\n\n\ #\ Second\ selection,\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ 2\ \ \ _\ \ \ _\ \ \ 1\ \ \ _\ \ \ _\ \ \ _\ \ <--\ order\ chosen\n\n\ #\ etc.\n\n\ #\ For\ 7\ elements\ and\ a\ counting\ interval\ of\ 4:\n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ <--\ number\n\ #\ \ 2\ \ \ 7\ \ \ 6\ \ \ 1\ \ \ 4\ \ \ 3\ \ \ 5\ \ <--\ order\ chosen\ \n\n\ #\ For\ 13\ elements\ and\ a\ counting\ interval\ of\ 3:\ \n\ #\ \ 1\ \ \ 2\ \ \ 3\ \ \ 4\ \ \ 5\ \ \ 6\ \ \ 7\ \ \ 8\ \ \ 9\ \ \ 10\ \ \ 11\ \ \ 12\ \ \ 13\n\ #\ 11\ \ \ 5\ \ \ 1\ \ \ 8\ \ 10\ \ \ 2\ \ \ 6\ \ 12\ \ \ 3\ \ \ \ 9\ \ \ \ 7\ \ \ \ 4\ \ \ 13\n\n\ #\ This\ is\ called\ the\ Ring\ of\ Josephus\ because,\ as\ one\ version\ of\n\ #\ the\ story\ goes,\ around\ 63\ A.D.\ 13\ soldiers\ formed\ a\ suicide\ pact\n\ #\ instead\ of\ surrendering\ to\ the\ Romans.\ They\ stood\ in\ a\ circle\ and\n\ #\ counted\ around\ by\ 3's\ killing\ each\ man\ in\ turn.\ The\ final\ man\n\ #\ standing\ was\ to\ commit\ suicide.\ Their\ leader,\ Josephus,\ had\n\ #\ cleverly\ chosen\ his\ position\ to\ be\ 'last'.\ Along\ with\ his\ best\n\ #\ friend\ who\ was\ in\ the\ 'next\ to\ last'\ position,\ they\ deserted\n\ #\ to\ the\ Roman\ army.\ \n\n\ #\ I'm\ told\ that\ there\ are\ applications\ in\ coding\ theory\ and\n\ #\ networking,\ but\ I've\ never\ found\ anything\ on\ the\ web.\n\ #\ I\ use\ this\ as\ an\ example\ of\ a\ problem\ that\ cannot\ be\ solved\n\ #\ algebraically.\ Except\ for\ a\ few\ special\ cases\ there\ is\ no\n\ #\ known\ formula\ that\ will\ predict\ when\ an\ element\ will\ be\n\ #\ chosen\ for\ an\ arbitrary\ number\ of\ elements\ and\ counting\ interval.\n\ #\ The\ answer\ must\ be\ determined\ by\ construction\ (programming.)\n======\n''lp''\n======\n\ #\ Program\ to\ generate\ Ring\ of\ Josephus\ by\ DKF\n\ proc\ ROJ\ \{\{n\ 13\}\ \{skip\ 3\}\}\ \{\n\ \ \ \ set\ list\ \{\}\n\ \ \ \ set\ result\ \{\}\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ lappend\ list\ \$i\n\ \ \ \ \}\n\ \ \ \ set\ i\ 0\n\ \ \ \ while\ \{\[llength\ \$list\]\}\ \{\n\ \ \ \ \ \ \ set\ i\ \[expr\ \{(\$i+\$skip)%\[llength\ \$list\]\}\]\n\ \ \ \ \ \ \ lappend\ result\ \[lindex\ \$list\ \$i\]\n\ \ \ \ \ \ \ set\ list\ \[lreplace\ \$list\ \$i\ \$i\]\n\ \ \ \ \}\n\ \ \ \ return\ \$result\n\ \}\n\ proc\ PrintRingOfJosephus\ \{ringSize\ skipNumber\}\ \{\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i<=\$ringSize\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ puts\ -nonewline\ \[format\ %4d\ \$i\]\n\ \ \ \ \}\n\ \ \ \ puts\ \"\"\n\ \ \ \ foreach\ i\ \[ROJ\ \$ringSize\ \$skipNumber\]\ \{\n\ \ \ \ \ \ \ puts\ -nonewline\ \[format\ %4d\ \$i\]\n\ \ \ \ \}\n\ \ \ \ puts\ \"\"\n\ \}\n======\n----\n\n**A\ program\ that\ prints\ the\ partitions\ of\ a\ number**\n======\n\ #\ The\ partitions\ of\ a\ number\ are\ simply\ the\ different\ ways\ a\ \n\ #\ number\ can\ be\ summed.\ (Summands\ are\ in\ descending\ order.)\n\ #\ There\ are\ 7\ partitions\ of\ 5:\ \ 5,\ 41,\ 32,\ 311,\ 221,\ 2111,\ 11111\n\ #\ There\ are\ 14\ partitions\ of\ 7:\n\ #\ 7,\ 61,\ 52,\ 511,\ 43,\ 421,\ 4111,\ 322,\ 3211,\n\ #\ 3111,\ 2221,\ 22111,\ 211111,\ 1111111\n\ #\ The\ number\ of\ partitions\ becomes\ large\ very\ quickly.\ Although\n\ #\ simple,\ partitions\ are\ of\ theoretical\ interest\ in\ number\n\ #\ theory\ and\ physics.\n======\n''lp''\n\n\[KBK\]\ (17\ October\ 2000):\ Here's\ one\ possibility:\n======\n\ #----------------------------------------------------------------------\n\ #\n\ #\ part\ --\n\ #\n\ #\ \ \ \ \ \ \ List\ the\ partitions\ of\ a\ number,\ optionally\ limiting\ the\ size\n\ #\ \ \ \ \ \ \ of\ the\ largest\ element.\n\ #\n\ #\ Parameters:\n\ #\ \ \ \ \ \ \ n\ --\ Number\ to\ be\ partitioned\n\ #\ \ \ \ \ \ \ k\ --\ (Optional)\ Size\ of\ the\ largest\ element.\ \ Default\ is\ n.\n\ #\n\ #\ Results:\n\ #\ \ \ \ \ \ \ Returns\ a\ list\ of\ the\ partitions\ of\ n\ with\ no\ element\ larger\n\ #\ \ \ \ \ \ \ than\ k,\ in\ reverse\ lexicographic\ order.\n\ #\n\ #\ Side\ effects:\n\ #\ \ \ \ \ \ \ None.\n\ #\n\ #----------------------------------------------------------------------\n\ \n\ proc\ part\ \{\ n\ \{\ k\ -1\ \}\ \}\ \{\n\ \n\ \ \ \ \ #\ If\ k\ is\ not\ supplied,\ or\ if\ k\ is\ greater\ than\ n\ (no\ partition\n\ \ \ \ \ #\ of\ n\ can\ have\ an\ element\ greater\ than\ n),\ set\ k\ to\ the\ default\n\ \ \ \ \ #\ of\ n.\n\ \n\ \ \ \ \ if\ \{\ \$k\ <\ 0\ ||\ \$k\ >\ \$n\ \}\ \{\n\ \ \ \ \ \ \ \ \ set\ k\ \$n\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ There\ is\ only\ one\ partition\ of\ zero:\ the\ empty\ list.\n\ \n\ \ \ \ \ if\ \{\ \$n\ ==\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ return\ \[list\ \[list\]\]\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ Accumulate\ the\ list\ of\ partitions.\ \ This\ is\ done\ by\ choosing\n\ \ \ \ \ #\ the\ largest\ element\ r\ of\ the\ partition,\ then\ partitioning\n\ \ \ \ \ #\ the\ remaining\ n-r\ so\ that\ no\ element\ is\ greater\ than\ r.\n\ \n\ \ \ \ \ set\ parts\ \{\}\n\ \ \ \ \ for\ \{\ set\ r\ \$k\ \}\ \{\ \$r\ >\ 0\ \}\ \{\ incr\ r\ -1\ \}\ \{\n\ \ \ \ \ \ \ \ \ foreach\ list\ \[part\ \[expr\ \{\ \$n\ -\ \$r\ \}\]\ \$r\]\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ lappend\ parts\ \[linsert\ \$list\ 0\ \$r\]\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \}\n\ \n\ \ \ \ \ #\ Return\ the\ accumulated\ list.\n\ \n\ \ \ \ \ return\ \$parts\n\ \}\n\ \n\ #\ Main\ program\ to\ demonstrate\ partitioning\ of\ integers.\n\ \n\ for\ \{\ set\ i\ 0\ \}\ \{\ \$i\ <=\ 7\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ puts\ \"Partitions\ of\ \$i:\"\n\ \ \ \ \ foreach\ list\ \[part\ \$i\]\ \{\n\ \ \ \ \ \ \ \ \ puts\ \$list\n\ \ \ \ \ \}\n\ \}\n======\n----\n**A\ program\ to\ compute\ pi\ using\ Monte-Carlo\ methods**\n======\n\ #\ x^2\ +\ y^2\ =\ 1\ is\ a\ circle\ of\ radius\ 1.\n\ #\ Many\ random\ coordinates\ for\ x\ and\ y\ are\ generated\n\ #\ in\ the\ first\ quadrant\ of\ the\ x-y\ plane\ and\ tested\n\ #\ to\ see\ whether\ the\ points\ fall\ inside\ the\ circle.\n\ #\ The\ area\ in\ quad\ I\ for\ a\ unit\ circle\ is\ pi/4.\n\ #\ The\ ratio\ of\ the\ pairs\ inside\ the\ circle\ to\ the\n\ #\ total\ number\ of\ points\ tested\ approaches\ pi/4\ when\n\ #\ the\ number\ of\ random\ points\ is\ large\ enough.\ \ \n\ \n\ set\ n\ 1000000\n\ set\ count\ 0\n\ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ \ set\ x\ \[expr\ rand()\]\n\ \ \ \ \ \ \ \ set\ y\ \[expr\ rand()\]\n\ \ \ \ \ \ \ \ \ if\ \{\[expr\ \{(\$x*\$x\ +\ \$y*\$y)\ <\ 1\}\]\}\ \{\n\ \ \ \ \ \ \ \ \ incr\ count\ \}\n\ \ \ \ \}\n\ puts\ \"Number\ of\ points\ inside\ the\ circle=\ \ \$count\"\n\ puts\ \"Total\ points\ =\ \$n\"\n\ \n\ set\ pi\ \[expr\ 4e+0*\$count/\$n\]\n\ puts\ \"Approximation\ for\ pi\ is\ \$pi\"\n\ \ \n\ #\ For\ 1,000,000\ points\ pi\ is\ computed\ correct\ to\ 2\ decimal\ places.\n\ #\ Below\ is\ the\ result\ for\ 5\ trials.\ My\ sensibilities\ tell\ me\ that\n\ #\ the\ approximation\ should\ be\ better,\ and\ more\ convergent.\n\ #\ Any\ ideas\ on\ why\ there\ is\ so\ much\ divergence\ in\ the\ results?\n\ #\ Am\ I\ handling\ the\ \[rand\]\ function\ incorrectly?\n\ #\ (Even\ for\ awk,\ there\ is\ 3\ place\ decimal\ accuracy\ for\ n=1M.)\n\ #\ Is\ it\ due\ to\ the\ fact\ that\ rand()\ uses\ the\ clock\ and\n\ #\ does\ not\ generate\ 'true'\ random\ numbers?\n\ #\ Should\ I\ be\ using\ srand()?\n\ #\n\ #\ Also,\ it\ was\ necessary\ to\ use\ \[expr\ 4e+0*\$count/\$n\]\n\ #\ to\ get\ a\ floating\ point\ answer.\ \[expr\ \$count/\$n\]\ gives\ 3.\n\ #\ There\ seem\ to\ be\ some\ subtleties\ in\ this\ simple\ program\n\ #\ that\ I\ don't\ feel\ qualified\ to\ explain.\n\ #\ Any\ thoughts\ would\ be\ appreciated.\n\n\ #\ 5\ trials\ for\ tcl/tk:\n\ #\ points\ inside:\ 785907,\ \ \ 785725,\ 785062,\ \ \ 785194,\ \ \ 785041\n\ #\ approx\ for\ pi:\ 3.143628,\ 3.1429,\ 3.140248,\ 3.140776,\ 3.140164\ \n======\n''lp''\n\n---\n\nSorry\ to\ report:\ your\ sensibilities\ are\ \"wrong\",\ the\ method\ is\ really\ not\ that\ accurate!\n\nIf\ you\ compute\ the\ standard\ deviation\ of\ your\ estimate\ (assuming\ that\ the\ n\ points\ that\ you\ generate\ are\ uniformly\ distributed\ in\ the\ unit\ square),\ you\ obtain\ \n\ \ \ s\ =\ sqrt(pi(4-pi)/n)\ ~\ 1.64/sqrt(n)\n\nIf\ piE\ is\ the\ resulting\ estimate\ for\ n=1E6,\ this\ means\ that\ \n\ \ \ *\ |piE\ -\ pi|\ <\ \ s\ ~\ 1.64E-3\ \ with\ probability\ 0.68\ \n\ \ \ *\ |piE\ -\ pi|\ <\ 2s\ ~\ 3.28E-3\ \ with\ probability\ 0.95\n\ \ \ *\ |piE\ -\ pi|\ <\ 3s\ ~\ 4.92E-3\ \ with\ probability\ 0.997\n\nSo:\ you\ have\ no\ right\ to\ expect\ better\ than\ two\ decimal\ digits\ accuracy\ with\ this\ method\ and\ a\ million\ points.\n\nRemark\ that\ if\ you\ use\ 5\ million\ points\ (i.e.,\ if\ you\ average\ your\ 5\ results),\ the\ resulting\ standard\ deviation\ goes\ down\ to\ \n\ \ s\ ~\ 1.64E-3/sqrt(5)\ ~\ 7.3E-4\ngiving\ a\ reasonable\ probability\ of\ hitting\ the\ third\ decimal\ digit\ ...\n\n''ms''\n\n---\n\nI\ bow\ to\ your\ lamp,\ sir!\ Thanks\ for\ the\ reminder\ to\ check\ the\ math\nbefore\ jumping\ to\ conclusions\ about\ the\ code.\n\nMore\ info\ on\ the\ rand\ function\ is\ here:\ \[rand\]\n\n''lp''\n\n---\n\n''DKF''\ -\ Point\ of\ Tcl\ style:\n\nIt\ is\ better\ to\ write\ the\ above\ code\ as:\n======\n\ proc\ montecarlo-pi\ \{n\}\ \{\n\ \ \ \ set\ count\ 0\n\ \ \ \ for\ \{set\ i\ 1\}\ \{\$i\ <=\ \$n\}\ \{incr\ i\}\ \{\n\ \ \ \ \ \ \ set\ x\ \[expr\ \{rand()\}\]\n\ \ \ \ \ \ \ set\ y\ \[expr\ \{rand()\}\]\n\ \ \ \ \ \ \ if\ \{(\$x*\$x\ +\ \$y*\$y)\ <\ 1\}\ \{\n\ \ \ \ \ \ \ \ \ \ incr\ count\n\ \ \ \ \ \ \ \}\n\ \ \ \ \}\n\ \ \ \ expr\ \{4e0*\$count/\$n\}\n\ \}\n\ puts\ \"Approximation\ for\ pi\ is\ \[montecarlo-pi\ 1000000\]\"\n======\n----\n**A\ simple\ calculator**\n\n\[Arjen\ Markus\]\ '''A\ simple\ calculator'''\ (two\ types)\ can\ be\ found\ at\ \[A\ little\ math\ language\]\n----\n\n**compute\ first\ thousand\ digits\ of\ e**\n\n\[GS\]\ Here\ is\ a\ small\ program\ that\ computes\ 1000\ digits\ of\ ''e''\ using\ a\ spigot\ algorithm\ (see\ also\ \[e\]):\n======\n\ #\ 1000\ digits\ of\ e\ with\ a\ spigot\ algorithm\n\ \n\ proc\ e1\ \{\}\ \{\n\ \ set\ f\ \{\}\n\ \ set\ n\ 1000\n\ \ set\ b\ \[expr\ \{10\ *\ \$n\ /\ 4\}\]\n\ \ for\ \{set\ i\ 0\}\ \{\$i\ <=\ \$b\}\ \{incr\ i\}\ \{lappend\ f\ 1\}\ \ \ \ \ \n\ \ incr\ n\ -1\n\ \ puts\ -nonewline\ \"2.\"\n\ \ for\ \{set\ j\ 1\}\ \{\$j\ <=\ \$n\}\ \{incr\ j\}\ \{\n\ \ \ \ \ set\ q\ 0\n\ \ \ \ \ for\ \{set\ i\ \$b\}\ \{\$i\ >=\ 1\}\ \{incr\ i\ -1\}\ \{\n\ \ \ \ \ \ \ \ set\ k\ \[expr\ \{\$i\ +\ 1\}\]\n\ \ \ \ \ \ \ \ set\ x\ \[expr\ \{\[lindex\ \$f\ \$i\]*10\ +\ \$q\}\]\n\ \ \ \ \ \ \ \ lset\ f\ \$i\ \[expr\ \{\$x\ %\ \$k\}\]\n\ \ \ \ \ \ \ \ set\ q\ \[expr\ \{\$x\ /\ \$k\}\]\n\ \ \ \ \ \}\n\ \ \ \ \ puts\ -nonewline\ \$q\n\ \ \ \ \ flush\ stdout\n\ \ \}\n\ \}\n\ \n\ e1\n\n======\nReference:\n\n-\ A.\ H.\ J.\ Sale,\ ''The\ Calculation\ of\ e\ to\ Many\ Significant\ Digits'',\ The\ Computer\ Journal\ (1968)\ 11\ (2):\ pp229-230\ \[http://comjnl.oxfordjournals.org/content/11/2/229.full.pdf+html\]\n\n-\ Stanley\ Rabinowitz,\ Stanley\ Wagon,\ ''A\ Spigot\ Algorithm\ for\ the\ Digits\ of\ Pi'',\ American\ Mathematical\ Monthly\ 102\ (3),\ 1995,\ pp195–203\ \[http://www.mathpropress.com/stan/bibliography/spigot.pdf\]\n======\n\n----\n**extended\ eucleian\ algorithm**\n\n\[aleksanteri\]/arezey,\ 17th\ of\ September\ 2008:\ A\ 61LOC\ implementation\ of\ the\ extended\ Euclerian\ Algorithm.\n\n======\n\ proc\ eea\ \{a\ b\}\ \{\n\ \ \ \ \ \ \ \ \ set\ r\ 1\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ x\ \$a\n\ \ \ \ \ \ \ \ \ set\ y\ \$b\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \{\}\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ while\ \{\$r\ !=\ 0\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ r\ \[expr\ \$x%\$y\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ q\ \[expr\ (\$x-\$r)/\$y\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ puts\ \"\$x/\$y\ =\ \$q\ r\ \$r\\t\$x\ =\ \$y*\$q+\$r\"\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ exprs\ \[linsert\ \$exprs\ 0\ \[list\ \$r\ \$x\ \$y\ \$q\]\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$r\ ==\ 0\ &&\ !\[info\ exists\ gcd\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ gcd\ 1\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ x\ \$y\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ y\ \$r\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{!\[info\ exists\ gcd\]\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{(\$x%\$y)\ ==\ 0\}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ gcd\ \$y\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \[lreplace\ \$exprs\ 0\ 0\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ puts\ \"gcd(\$a,\$b)\ =\ \$gcd\"\n\ \ \ \ \ \ \ \ \ puts\ \"-------------\"\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ pn\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 1\]\n\ \ \ \ \ \ \ \ \ set\ pf\ 1\n\ \ \ \ \ \ \ \ \ set\ nn\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 2\]\n\ \ \ \ \ \ \ \ \ set\ nf\ \[lindex\ \[lindex\ \$exprs\ 0\]\ 3\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ exprs\ \[lreplace\ \$exprs\ 0\ 0\]\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ set\ c\ p\n\ \ \ \ \ \ \ \ \ set\ o\ n\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ puts\ \"\$gcd\ =\ \$pn*\$pf\ -\ \$nn*\$nf\"\n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ foreach\ n\ \$exprs\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\$c\ eq\ \"p\"\}\ \{set\ c\ n\;\ set\ o\ p\}\ else\ \{set\ c\ p\;\ set\ o\ n\}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 0\ \[lindex\ \$n\ 0\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 1\ \[lindex\ \$n\ 1\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 2\ \[lindex\ \$n\ 2\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ 3\ \[lindex\ \$n\ 3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ \$\{c\}n\ \$1\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ set\ \$\{o\}f\ \[expr\ \$\$\{o\}f+(\$\$\{c\}f*\$3)\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ puts\ \"\$gcd\ =\ \$pn*\$pf\ -\ \$nn*\$nf\"\n\ \ \ \ \ \ \ \ \ \}\n\ \}\n======\n\n\[Lars\ H\],\ 2008-09-18:\ Urgh!\ That's\ much\ too\ complicated.\ 11\ lines\ and\ only\ one\ loop\ are\ quite\ sufficient:\n\n======\n\ proc\ xgcd\ \{a\ b\}\ \{\n\ \ \ \ set\ r0\ 1\;\ set\ s0\ 0\n\ \ \ \ set\ r1\ 0\;\ set\ s1\ 1\n\ \ \ \ while\ \{\$b\}\ \{\n\ \ \ \ \ \ \ set\ q\ \[expr\ \{\$a\ /\ \$b\}\]\n\ \ \ \ \ \ \ set\ b\ \[expr\ \{\$a\ -\ \$q*\[set\ a\ \$b\]\}\]\n\ \ \ \ \ \ \ set\ r1\ \[expr\ \{\$r0\ -\ \$q*\[set\ r0\ \$r1\]\}\]\n\ \ \ \ \ \ \ set\ s1\ \[expr\ \{\$s0\ -\ \$q*\[set\ s0\ \$s1\]\}\]\n\ \ \ \ \}\n\ \ \ \ list\ \$a\ \$r0\ \$s0\n\ \}\n======\n\nTo\ demonstrate\ as\ a\ program:\n\n======\n\ proc\ eea\ \{a\ b\}\ \{\n\ \ \ \ foreach\ \{d\ r\ s\}\ \[xgcd\ \$a\ \$b\]\ break\n\ \ \ \ puts\ \"\$d\ =\ \$a*\$r\ +\ \$b*\$s\"\n\ \}\n======\n\nHow\ it\ works\ (beyond\ the\ ordinary\ \[greatest\ common\ denominator\])\ is\ that\n\ \ (\ r0\ \ s0\ )\n\ \ (\ r1\ \ s1\ )\nis\ a\ matrix\ which,\ when\ multiplied\ by\ the\ initial\ (''a'',''b'')\ as\ a\ column\ vector\ on\ the\ right,\ computes\ the\ current\ (''a'',''b''):\n\ \ (\ a\ )\ =\ (\ r0\ \ s0\ )\ (\ a_initial\ )\n\ \ (\ b\ )\ \ \ (\ r1\ \ s1\ )\ (\ b_initial\ )\nSince\ the\ very\ last\ ''a''\ is\ the\ gcd,\ the\ first\ row\ of\ the\ matrix\ gives\ the\ coefficients\ which\ express\ it\ as\ a\ linear\ combination\ of\ the\ arguments.\ The\ three\ lines\ on\ the\ form\n\ \ \ set\ b\ \[expr\ \{\$a\ -\ \$q*\[set\ a\ \$b\]\}\]\nall\ amount\ to\ multiplying\ a\ column\ vector\ on\ the\ left\ by\ a\ matrix\ on\ the\ form\n\ \ (\ 0\ \ 1\ )\n\ \ (\ 1\ -q\ )\n\n----\n***Screenshots***\n\n\[http://farm5.static.flickr.com/4048/4695664749_1e35692ca4.jpg\]\n\n\[gold\]\ added\ pix,\ for\ math\ expression\ table\ script\ above\ \ \ \n\n<<categories>>\ Mathematics|Example} CALL {my revision {Sample Math Programs}} CALL {::oo::Obj3025239 process revision/Sample+Math+Programs} CALL {::oo::Obj3025237 process}

-errorcode

NONE

-errorinfo

Unknow state transition: LINE -> END
    while executing
"error $msg"
    (class "::Wiki" method "render_wikit" line 6)
    invoked from within
"my render_$default_markup $N $C $mkup_rendering_engine"
    (class "::Wiki" method "render" line 8)
    invoked from within
"my render $name $C"
    (class "::Wiki" method "revision" line 31)
    invoked from within
"my revision $page"
    (class "::Wiki" method "process" line 56)
    invoked from within
"$server process [string trim $uri /]"

-errorline

4