Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
8234415046525 | ||
8234415286141 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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