From 4d544e733fca47b09914e2241e1bf8633ec6968a Mon Sep 17 00:00:00 2001 From: def Date: Mon, 28 Jul 2014 02:48:38 +0200 Subject: [PATCH] RC --- gost2814789.nim | 27 +++++++++++++++++++++++++++ gost2814789.out | 2 ++ magicsquares.nim | 24 ++++++++++++++++++++++++ magicsquares.out | 30 ++++++++++++++++++++++++++++++ test_all.nim | 2 ++ 5 files changed, 85 insertions(+) create mode 100644 gost2814789.nim create mode 100644 gost2814789.out create mode 100644 magicsquares.nim create mode 100644 magicsquares.out diff --git a/gost2814789.nim b/gost2814789.nim new file mode 100644 index 0000000..70ef566 --- /dev/null +++ b/gost2814789.nim @@ -0,0 +1,27 @@ +var + k8 = [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7] + k7 = [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10] + k6 = [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8] + k5 = [ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15] + k4 = [ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9] + k3 = [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11] + k2 = [ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1] + k1 = [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7] + + k87, k65, k43, k21 = newSeq[int](256) + +proc kboxInit = + for i in 0 .. 255: + k87[i] = k8[i shr 4] shl 4 or k7[i and 15] + k65[i] = k6[i shr 4] shl 4 or k5[i and 15] + k43[i] = k4[i shr 4] shl 4 or k3[i and 15] + k21[i] = k2[i shr 4] shl 4 or k1[i and 15] + +proc f(x): int = + let x = k87[x shr 24 and 255] shl 24 or k65[x shr 16 and 255] shl 16 or + k43[x shr 8 and 255] shl 8 or k21[x and 255] + x shl 11 or x shr (32 - 11) + +kboxInit() +echo f(15) +echo f(30) diff --git a/gost2814789.out b/gost2814789.out new file mode 100644 index 0000000..0efad36 --- /dev/null +++ b/gost2814789.out @@ -0,0 +1,2 @@ +8234415046525 +8234415286141 diff --git a/magicsquares.nim b/magicsquares.nim new file mode 100644 index 0000000..2dc5edd --- /dev/null +++ b/magicsquares.nim @@ -0,0 +1,24 @@ +import strutils + +proc `^`*(base: int, exp: int): int = + var (base, exp) = (base, exp) + result = 1 + + while exp != 0: + if (exp and 1) != 0: + result *= base + exp = exp shr 1 + base *= base + +proc magic(n) = + for row in 1 .. n: + for col in 1 .. n: + let cell = (n * ((row + col - 1 + n div 2) mod n) + + ((row + 2 * col - 2) mod n) + 1) + stdout.write align($cell, len($(n^2)))," " + echo "" + echo "\nAll sum to magic number ", ((n * n + 1) * n div 2) + +for n in [5, 3, 7]: + echo "\nOrder ",n,"\n=======" + magic(n) diff --git a/magicsquares.out b/magicsquares.out new file mode 100644 index 0000000..f06af22 --- /dev/null +++ b/magicsquares.out @@ -0,0 +1,30 @@ + +Order 5 +======= +17 24 1 8 15 +23 5 7 14 16 + 4 6 13 20 22 +10 12 19 21 3 +11 18 25 2 9 + +All sum to magic number 65 + +Order 3 +======= +8 1 6 +3 5 7 +4 9 2 + +All sum to magic number 15 + +Order 7 +======= +30 39 48 1 10 19 28 +38 47 7 9 18 27 29 +46 6 8 17 26 35 37 + 5 14 16 25 34 36 45 +13 15 24 33 42 44 4 +21 23 32 41 43 3 12 +22 31 40 49 2 11 20 + +All sum to magic number 175 diff --git a/test_all.nim b/test_all.nim index 8238961..760326f 100644 --- a/test_all.nim +++ b/test_all.nim @@ -180,6 +180,7 @@ testIt "gameoflife": check it.compiles testIt "globallyreplacetext": check it.compiles testIt "gltest": check it.compiles testIt "gnomesort": check it.returns +testIt "gost2814789": check it.returns testIt "gray": check it.returns testIt "guessthenumber": check it.compiles testIt "guessthenumberplayer": check it.compiles @@ -241,6 +242,7 @@ testIt "loopmult": check it.returns testIt "loop": check it.returns testIt "lucaslehmertest": check it.returns testIt "luhntest": check it.returns +testIt "magicsquares": check it.returns testIt "mail": check it.compiles("-d:ssl") testIt "manboy": check it.returns testIt "mandel": check it.returns