Last active
April 14, 2022 15:00
-
-
Save extemporalgenome/33cecccc7a5d998ac6c21a22838fc8fb to your computer and use it in GitHub Desktop.
Comparing modulo arithmetic across languages
This file contains 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
// https://en.wikipedia.org/wiki/Modular_arithmetic#Examples | |
// https://dartpad.dev/?id=33cecccc7a5d998ac6c21a22838fc8fb | |
void main() { | |
congruence(38, 14, mod: 12); | |
congruence(2, -3, mod: 5); | |
congruence(-8, 7, mod: 5); | |
congruence(-3, -8, mod: 5); | |
} | |
void congruence(int x, y, {required int mod}) { | |
String sym = (x % mod) == (y % mod) ? "≡" : "≢"; | |
print("$x $sym $y (euclidean mod $mod)"); | |
} | |
/* Output: | |
38 ≡ 14 (euclidean mod 12) | |
2 ≡ -3 (euclidean mod 5) | |
-8 ≡ 7 (euclidean mod 5) | |
-3 ≡ -8 (euclidean mod 5) | |
*/ |
This file contains 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
-- https://en.wikipedia.org/wiki/Modular_arithmetic#Examples | |
-- https://ellie-app.com/hd32NnLQSFqa1 | |
-- Output: | |
-- 38 ≡ 14 (flooring mod 12) | |
-- 2 ≢ -3 (flooring mod 5) | |
-- -8 ≢ 7 (flooring mod 5) | |
-- -3 ≢ -8 (flooring mod 5) | |
-- | |
-- 38 ≡ 14 (truncating mod 12) | |
-- 2 ≢ -3 (truncating mod 5) | |
-- -8 ≡ 7 (truncating mod 5) | |
-- -3 ≢ -8 (truncating mod 5) | |
module Main exposing (main) | |
import Html exposing (Html, pre, text) | |
import List | |
import String | |
equivalences = | |
[ { x = 38, y = 14, mod = 12 } | |
, { x = 2, y = -3, mod = 5 } | |
, { x = -8, y = 7, mod = 5 } | |
, { x = -3, y = -8, mod = 5 } | |
] | |
type alias Expr = | |
{ x : Int | |
, y : Int | |
, mod : Int | |
} | |
main = | |
view equivalences | |
view : List Expr -> Html msg | |
view exprs = | |
let | |
modCongruence = | |
congruence modBy "flooring mod" | |
remCongruence = | |
congruence remainderBy "truncating mod" | |
in | |
pre [] <| | |
List.map modCongruence exprs | |
++ [ text "\n" ] | |
++ List.map remCongruence exprs | |
congruence : (Int -> Int -> Int) -> String -> Expr -> Html msg | |
congruence fun label expr = | |
let | |
{ x, y, mod } = | |
expr | |
sym = | |
if fun x mod == fun y mod then | |
"≡" | |
else | |
"≢" | |
in | |
text <| format label sym expr | |
format : String -> String -> Expr -> String | |
format label sym { x, y, mod } = | |
let | |
xStr = | |
String.fromInt x | |
yStr = | |
String.fromInt y | |
modStr = | |
String.fromInt mod | |
in | |
xStr | |
++ " " | |
++ sym | |
++ " " | |
++ yStr | |
++ " (" | |
++ label | |
++ " " | |
++ modStr | |
++ ")\n" |
This file contains 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
// https://en.wikipedia.org/wiki/Modular_arithmetic#Examples | |
// https://go.dev/play/p/kPja5gaImqW | |
package main | |
import "fmt" | |
func main() { | |
congruence(expr{38, 14, 12}) | |
congruence(expr{2, -3, 5}) | |
congruence(expr{-8, 7, 5}) | |
congruence(expr{-3, -8, 5}) | |
} | |
type expr struct{ x, y, mod int } | |
func congruence(e expr) { | |
sym := '≢' | |
if e.x%e.mod == e.y%e.mod { | |
sym = '≡' | |
} | |
fmt.Printf("%d %c %d (truncating mod %d)\n", e.x, sym, e.y, e.mod) | |
} | |
/* Output: | |
38 ≡ 14 (truncating mod 12) | |
2 ≢ -3 (truncating mod 5) | |
-8 ≢ 7 (truncating mod 5) | |
-3 ≡ -8 (truncating mod 5) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment