Created
October 9, 2019 22:11
-
-
Save ratfactor/3af4b527e943a4f7b812b6b1598ba23f to your computer and use it in GitHub Desktop.
the new decomposition method of adding - 3rd grade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, strutils, math | |
if paramCount() < 2: | |
echo "Error: supply two integers" | |
quit(QuitFailure) | |
var a, b: int | |
var hundreds, tens, ones: int | |
try: | |
a = parseInt(paramStr(1)) | |
b = parseInt(paramStr(2)) | |
except ValueError: | |
echo "Error: one of those was not an integer" | |
quit(QuitFailure) | |
let answer = a+b | |
echo "Problem: ", a, " plus ", b, " equals ", answer | |
echo "Decompose with math:" | |
hundreds = (a div 100 * 100) | |
a -= hundreds | |
tens = (a div 10 * 10) | |
a -= tens | |
ones = a | |
echo hundreds, "+", tens, "+", ones | |
echo "Decompose with strings:" | |
proc get_place(s:string, place:int, mult:int): int= | |
let digit:char = s[s.len - place] | |
return parseInt( $digit ) * mult | |
let x = paramStr(1) | |
let y = paramStr(2) | |
hundreds = get_place(x, 3, 100) + get_place(y, 3, 100) | |
tens = get_place(x, 2, 10) + get_place(y, 2, 10) | |
ones = get_place(x, 1, 1) + get_place(y, 1, 1) | |
let answer2 = hundreds+tens+ones | |
if answer != answer2: | |
echo "Error: answers not equal!" | |
echo hundreds, " + ", tens, " + ", ones, " = ", (hundreds+tens+ones) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment