Skip to content

Commit 2288056

Browse files
committed
Examples: Fix compiling issues and almost all imports
1 parent bc3b2fa commit 2288056

13 files changed

+117
-61
lines changed

examples/elliptic.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# By Cyther606: http://forum.nimrod-lang.org/t/522
22
# Adapted from: https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart4-PrivateKeyToPublicKey.py
3-
import bigints, math, strutils
3+
import bigints
4+
import std/[math, strutils]
5+
6+
const
7+
one = 1.initBigInt
48

59
proc `^`(base: int; exp: int): BigInt =
610
let base = base.initBigInt
711
var exp = exp
8-
result = 1.initBigInt
12+
result = one
913
while exp > 0:
1014
result *= base
1115
dec(exp)

examples/pidigits.nim

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
# Translation of http://benchmarksgame.alioth.debian.org/u32/program.php?test=pidigits&lang=go&id=4
22

3-
import os, strutils, unsigned, bigints
3+
import std/[os, strutils]
4+
import ../src/bigints
5+
6+
const
7+
one = 1.initBigInt
8+
zero = 0.initBigInt
9+
ten = 10.initBigInt
10+
two = 2.initBigInt
11+
let
12+
mask = (one shl 32) - one
413

514
var
6-
tmp1, tmp2, tmp3, acc, k, dd = initBigInt(0)
7-
den, num, k2 = initBigInt(1)
15+
tmp1, tmp2, tmp3, acc, k, dd = zero
16+
den, num, k2 = one
817

918
proc extractDigit(): int32 =
1019
if num > acc:
@@ -20,16 +29,16 @@ proc extractDigit(): int32 =
2029
if tmp2 >= den:
2130
return -1
2231

23-
result = int32(tmp1.limbs[0])
32+
result = cast[int32](tmp1 and mask)
2433

2534
proc eliminateDigit(d: int32) =
26-
acc -= den * d
27-
acc *= 10
28-
num *= 10
35+
acc -= den * d.initBigInt
36+
acc *= ten
37+
num *= ten
2938

3039
proc nextTerm() =
31-
k += 1
32-
k2 += 2
40+
k += one
41+
k2 += two
3342
tmp1 = num shl 1
3443
acc += tmp1
3544
acc *= k2

examples/rc_combperm.nim

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Solution for http://rosettacode.org/wiki/Combinations_and_permutations
2-
import bigints
2+
import ../src/bigints
3+
4+
const
5+
zero = 0.initBigInt
36

47
proc perm(n, k: int32): BigInt =
58
result = initBigInt 1
69
var
7-
k = n - k
8-
n = n
10+
k = initBigInt(n - k)
11+
n = initBigInt(n)
912
while n > k:
1013
result *= n
1114
dec n
1215

1316
proc comb(n, k: int32): BigInt =
1417
result = perm(n, k)
15-
var k = k
16-
while k > 0:
18+
var k = initBigInt(k)
19+
while k > zero:
1720
result = result div k
1821
dec k
1922

examples/rc_godtheinteger.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Solution for http://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
2-
import bigints
2+
import ../src/bigints
33

44
var cache = @[@[1.initBigInt]]
55

@@ -14,7 +14,7 @@ proc cumu(n: int): seq[BigInt] =
1414
proc row(n: int): seq[BigInt] =
1515
let r = cumu n
1616
result = @[]
17-
for i in 0 .. <n:
17+
for i in 0 ..< n:
1818
result.add r[i+1] - r[i]
1919

2020
echo "rows:"

examples/rc_godtheinteger2.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Solution for http://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python
2-
import bigints
2+
import ../src/bigints
33

44
var p = @[1.initBigInt]
5+
const
6+
zero = 0.initBigInt
57

68
proc partitions(n: int): BigInt =
7-
p.add 0.initBigInt
9+
p.add zero
810

911
for k in 1..n:
1012
var d = n - k * (3 * k - 1) div 2

examples/rc_hammingnumbers.nim

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
# Translation of http://rosettacode.org/wiki/Hamming_numbers#Another_implementation_of_same_approach
22

3-
import bigints, math
3+
import ../src/bigints
4+
import std/math
5+
6+
const
7+
two = initBigInt(2)
8+
three = initBigInt(3)
9+
five = initBigInt(5)
410

511
proc hamming(limit: int): BigInt =
612
var
713
h = newSeq[BigInt](limit)
8-
x2 = initBigInt(2)
9-
x3 = initBigInt(3)
10-
x5 = initBigInt(5)
14+
x2 = two
15+
x3 = three
16+
x5 = five
1117
i, j, k = 0
1218
for i in 0..h.high: h[i] = initBigInt(1)
1319
14-
for n in 1 .. < limit:
20+
for n in 1 ..< limit:
1521
h[n] = min([x2, x3, x5])
1622
if x2 == h[n]:
1723
inc i
18-
x2 = h[i] * 2
24+
x2 = h[i] * two
1925
if x3 == h[n]:
2026
inc j
21-
x3 = h[j] * 3
27+
x3 = h[j] * three
2228
if x5 == h[n]:
2329
inc k
24-
x5 = h[k] * 5
30+
x5 = h[k] * five
2531

2632
result = h[h.high]
2733

examples/rc_integersequence.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import bigints
1+
import ../src/bigints
22

3+
let one = 1.initBigInt
34
var i = 0.initBigInt
45
while true:
5-
i += 1
6+
i += one
67
echo i

examples/rc_leftfactorials.nim

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import iterutils, bigints
1+
import iterutils
2+
import ../src/bigints
3+
4+
const
5+
one = 1.initBigInt
6+
zero = 0.initBigInt
27

38
proc lfact: iterator: BigInt =
49
result = iterator: BigInt =
5-
yield 0.initBigInt
10+
yield zero
611
var
7-
fact = 1.initBigInt
8-
sum = 0.initBigInt
9-
n = 1.initBigInt
12+
fact = one
13+
sum = zero
14+
n = one
1015
while true:
1116
sum += fact
1217
fact *= n
13-
n += 1
18+
n += one
1419
yield sum
1520

1621
echo "first 11:"

examples/rc_modexp.nim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
# Solution for http://rosettacode.org/wiki/Modular_exponentiation
22

3-
import bigints
3+
import ../src/bigints
4+
5+
const
6+
zero = 0.initBigInt
7+
one = 1.initBigInt
8+
two = 2.initBigInt
49

510
proc powmod(b, e, m: BigInt): BigInt =
6-
assert e >= 0
11+
assert e >= zero
712
var e = e
813
var b = b
9-
result = initBigInt(1)
10-
while e > 0:
11-
if e mod 2 == 1:
14+
result = one
15+
while e > zero:
16+
if e mod two == one:
1217
result = (result * b) mod m
13-
e = e div 2
18+
e = e div two
1419
b = (b.pow 2) mod m
1520

1621
var
1722
a = initBigInt("2988348162058574136915891421498819466320163312926952423791023078876139")
1823
b = initBigInt("2351399303373464486466122544523690094744975233415544072992656881240319")
24+
res = powmod(a, b, initBigInt(10).pow 40)
1925

20-
echo powmod(a, b, 10.pow 40)
26+
echo res
27+
doAssert("1527229998585248450016808958343740453059".initBigInt == res)

examples/rc_paraffins.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Solution for http://rosettacode.org/wiki/Paraffins
2-
import bigints
2+
import ../src/bigints
33

44
const
55
nMax: int32 = 250
@@ -15,7 +15,7 @@ for i in 2 .. nMax:
1515
proc choose(m: BigInt, k: int32): BigInt =
1616
result = m
1717
if k == 1: return
18-
for i in 1 .. < k:
18+
for i in 1 ..< k:
1919
result = result * (m + i) div (i + 1)
2020

2121
proc tree(br, n, l, sum: int32, cnt: BigInt) =

examples/rc_pidigits.nim

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# Solution for http://rosettacode.org/wiki/Pi
22

3-
import strutils, unsigned, bigints
3+
import std/strutils
4+
import ../src/bigints
5+
6+
const
7+
one = 1.initBigInt
8+
zero = 0.initBigInt
9+
ten = 10.initBigInt
10+
two = 2.initBigInt
11+
12+
let
13+
mask = (one shl 32) - one
414

515
var
6-
tmp1, tmp2, tmp3, acc, k, dd = initBigInt(0)
7-
den, num, k2 = initBigInt(1)
16+
tmp1, tmp2, tmp3, acc, k, dd = zero
17+
den, num, k2 = one
818

919
proc extractDigit(): int32 =
1020
if num > acc:
@@ -20,16 +30,16 @@ proc extractDigit(): int32 =
2030
if tmp2 >= den:
2131
return -1
2232

23-
result = int32(tmp1.limbs[0])
33+
result = cast[int32](tmp1 and mask)
2434

2535
proc eliminateDigit(d: int32) =
26-
acc -= den * d
27-
acc *= 10
28-
num *= 10
36+
acc -= den * d.initBigInt
37+
acc *= ten
38+
num *= ten
2939

3040
proc nextTerm() =
31-
k += 1
32-
k2 += 2
41+
k += one
42+
k2 += two
3343
tmp1 = num shl 1
3444
acc += tmp1
3545
acc *= k2

examples/rc_pow.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Solution for http://rosettacode.org/wiki/Arbitrary-precision_integers_(included)
22

3-
import bigints
3+
import ../src/bigints
4+
import std/math
45

5-
var x = 5.pow 4.pow 3.pow 2
6+
let five = 5.initBigInt
7+
8+
var x = five.pow (4 ^ (3 ^ 2)).int32
69
var s = $x
710

811
echo s[0..19]

examples/rc_sum35.nim

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import bigints
1+
import ../src/bigints
2+
3+
const
4+
one = 1.initBigInt
5+
two = 2.initBigInt
6+
ten = 10.initBigInt
27

38
proc sumMults(first: int32, limit: BigInt): BigInt =
4-
var last = limit - 1
9+
var last = limit - one
10+
var first = first.initBigInt
511
last -= last mod first
6-
(last div first) * (last + first) div 2
12+
(last div first) * (last + first) div two
713

814
proc sum35(n: BigInt): BigInt =
915
result = sumMults(3, n)
1016
result += sumMults(5, n)
1117
result -= sumMults(15, n)
1218

13-
var x = 1.initBigInt
19+
var x = one
1420
while x < "1000000000000000000000000000000".initBigInt:
1521
echo sum35 x
16-
x *= 10
22+
x *= ten

0 commit comments

Comments
 (0)