Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Created July 24, 2023 08:32
Show Gist options
  • Save ahoy-jon/5e77b3be322411eac35d3c71690bc10c to your computer and use it in GitHub Desktop.
Save ahoy-jon/5e77b3be322411eac35d3c71690bc10c to your computer and use it in GitHub Desktop.
#awk '
function chunk_mod(s) {
len = 6 # 6 digits in case of 32-bit awk version
result = 0
while (length(s) > 0) {
chunk = substr(s, 1, len)
s = substr(s, len + 1)
result = (result * (10 ^ length(chunk)) + chunk) % 97
}
return result
}
function assert_expected(actual, expected, message) {
if (BLOCK_ASSERTIONS) {
return
}
if (!length(message)) {
message = "An assertion failed."
}
if (actual != expected) {
printf "%s (expected: \"%s\", got \"%s\")\n", message, expected, actual > "/dev/stderr"
close("/dev/stderr")
exit 1
}
}
BEGIN {
PREC = "octal"
assert_expected(chunk_mod("12345678"), 3, "chunk_mod(\"12345678\")")
assert_expected(chunk_mod("12345678901234567890123456789012345678901234567890123456789012345678901234567890"),54, "chunk_mod(\"12345678901234567890123456789012345678901234567890123456789012345678901234567890\")")
assert_expected(chunk_mod("12345678901230"),84, "chunk_mod(\"12345678901230\")")
assert_expected(chunk_mod("200410000159081552302045152700"), 77, "chunk_mod(\"200410000159081552302045152700\")")
print 200410000159081552302045152700 % 97
print "All tests passed."
}
#'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment