Skip to content

Commit

Permalink
RC
Browse files Browse the repository at this point in the history
  • Loading branch information
def- committed Jul 28, 2014
1 parent f8b4058 commit 4d544e7
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 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)
2 changes: 2 additions & 0 deletions gost2814789.out
@@ -0,0 +1,2 @@
8234415046525
8234415286141
24 changes: 24 additions & 0 deletions 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)
30 changes: 30 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions test_all.nim
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d544e7

Please sign in to comment.