From 94142e3a2de7aa3770f48d528ee763e2ab9149ea Mon Sep 17 00:00:00 2001 From: Victor Payno Date: Mon, 15 Apr 2024 21:36:25 -0700 Subject: [PATCH] tcl/difference-of-squares: 1st iteration --- tcl/README.md | 1 + tcl/difference-of-squares/README.md | 7 +- .../difference-of-squares.tcl | 15 ++-- tcl/difference-of-squares/run-tests-tcl.txt | 68 +++++++++++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 tcl/difference-of-squares/run-tests-tcl.txt diff --git a/tcl/README.md b/tcl/README.md index c15fb72b..53de4825 100644 --- a/tcl/README.md +++ b/tcl/README.md @@ -25,3 +25,4 @@ - [reverse-string](./reverse-string/README.md) - [roman-numerals](./roman-numerals/README.md) - [scrabble-score](./scrabble-score/README.md) +- [difference-of-squares](./difference-of-squares/README.md) diff --git a/tcl/difference-of-squares/README.md b/tcl/difference-of-squares/README.md index 12c9d87c..62cdbc9d 100644 --- a/tcl/difference-of-squares/README.md +++ b/tcl/difference-of-squares/README.md @@ -30,4 +30,9 @@ Finding the best algorithm for the problem is a key skill in software engineerin ### Based on -Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file +Problem 6 at Project Euler - https://projecteuler.net/problem=6 + +### My Solution + +- [difference-of-squares.tcl](./difference-of-squares.tcl) +- [run-tests](./run-tests-tcl.txt) diff --git a/tcl/difference-of-squares/difference-of-squares.tcl b/tcl/difference-of-squares/difference-of-squares.tcl index 6986a715..5d26ffd1 100644 --- a/tcl/difference-of-squares/difference-of-squares.tcl +++ b/tcl/difference-of-squares/difference-of-squares.tcl @@ -1,11 +1,14 @@ -proc squareOfSum {n} { - throw {NOT_IMPLEMENTED} "Implement this procedure." +proc squareOfSum {number} { + return [expr ($number * ($number + 1) / 2) ** 2] } -proc sumOfSquares {n} { - throw {NOT_IMPLEMENTED} "Implement this procedure." +proc sumOfSquares {number} { + return [expr ($number * ($number + 1) * (2 * $number + 1) / 6)] } -proc differenceOfSquares {n} { - throw {NOT_IMPLEMENTED} "Implement this procedure." +proc differenceOfSquares {number} { + set square [squareOfSum $number] + set sum [sumOfSquares $number] + + return [expr {$square - $sum}] } diff --git a/tcl/difference-of-squares/run-tests-tcl.txt b/tcl/difference-of-squares/run-tests-tcl.txt new file mode 100644 index 00000000..8e757f19 --- /dev/null +++ b/tcl/difference-of-squares/run-tests-tcl.txt @@ -0,0 +1,68 @@ +Running automated test file(s): + + +=============================================================================== + +Running: ttclcheck -scan /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares +ttclcheck version: 0.18 Copyright 2013 Artur Trzewik +All Rights Reserved. + +GPL licensed software +warn: pass shallow +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl +warn: pass collect +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl +warn: pass report +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl: parsing file /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl:17: unknown variable ::tcltest::numTests <<$::tcltest::numTests>> +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/testHelpers.tcl: errors file: 1 +warn: parsing /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl: parsing file /home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl:2: possible unintended double substitution for expr use {} <<($number>> +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl:6: possible unintended double substitution for expr use {} <<($number>> +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl:13: expect variable 'square' as 'numeric' but is 'string {}' <<$square>> +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl:13: expect variable 'sum' as 'numeric' but is 'string {}' <<$sum>> +/home/vpayno/git_vpayno/exercism-workspace/tcl/difference-of-squares/difference-of-squares.tcl: errors file: 4 +errors all: 5 (13.89% lines) +lines: 36 in: 00:00:00 59 lines/sec: 610.17 +commands: 5 variables: 1 +signature params 9 unknown: 5 55.56% unknown variables 0.00% + +real 0m0.127s +user 0m0.103s +sys 0m0.025s + +=============================================================================== + +Running: nagelfar -s _ ./difference-of-squares.tcl +Checking file ./difference-of-squares.tcl +Line 2: W Expr without braces +Line 6: W Expr without braces + +real 0m0.034s +user 0m0.026s +sys 0m0.007s + +=============================================================================== + +Running: RUN_ALL=1 tclsh ./difference-of-squares.test +difference-of-squares.test: Total 9 Passed 9 Skipped 0 Failed 0 + +real 0m0.010s +user 0m0.008s +sys 0m0.002s + + +=============================================================================== + +Running: misspell . + +real 0m0.019s +user 0m0.023s +sys 0m0.008s + +=============================================================================== +