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
6 changed files
with
243 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,34 @@ | ||
import math, strfmt | ||
randomize() | ||
|
||
const | ||
maxN = 20 | ||
times = 1_000_000 | ||
|
||
proc factorial(n): float = | ||
result = 1 | ||
for i in 1 .. n: | ||
result *= i.float | ||
|
||
proc expected(n): float = | ||
for i in 1 .. n: | ||
result += factorial(n) / pow(n.float, i.float) / factorial(n - i) | ||
|
||
proc test(n, times): int = | ||
for i in 1 .. times: | ||
var | ||
x = 1 | ||
bits = 0 | ||
while (bits and x) == 0: | ||
inc result | ||
bits = bits or x | ||
x = 1 shl random(n) | ||
|
||
echo " n\tavg\texp.\tdiff" | ||
echo "-------------------------------" | ||
for n in 1 .. maxN: | ||
let cnt = test(n, times) | ||
let avg = cnt.float / times | ||
let theory = expected(n) | ||
let diff = (avg / theory - 1) * 100 | ||
printlnfmt "{:2} {:8.4f} {:8.4f} {:6.3f}%", n, avg, theory, diff |
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,48 @@ | ||
import math | ||
|
||
type | ||
Point = tuple[x, y: float] | ||
Circle = tuple[x, y, r: float] | ||
|
||
proc circles(p1, p2: Point, r: float): tuple[c1, c2: Circle] = | ||
if r == 0: raise newException(EInvalidValue, | ||
"radius of zero") | ||
if p1 == p2: raise newException(EInvalidValue, | ||
"coincident points gives infinite number of Circles") | ||
|
||
# delta x, delta y between points | ||
let (dx, dy) = (p2.x - p1.x, p2.y - p1.y) | ||
# dist between points | ||
let q = sqrt(dx*dx + dy*dy) | ||
if q > 2.0*r: raise newException(EInvalidValue, | ||
"separation of points > diameter") | ||
|
||
# halfway point | ||
let p3: Point = ((p1.x+p2.x)/2, (p1.y+p2.y)/2) | ||
# distance along the mirror line | ||
let d = sqrt(r*r - (q/2)*(q/2)) | ||
# One answer | ||
result.c1 = (p3.x - d*dy/q, p3.y + d*dx/q, abs(r)) | ||
# The other answer | ||
result.c2 = (p3.x + d*dy/q, p3.y - d*dx/q, abs(r)) | ||
|
||
const tries: seq[tuple[p1, p2: Point, r: float]] = | ||
@[((0.1234, 0.9876), (0.8765, 0.2345), 2.0), | ||
((0.0000, 2.0000), (0.0000, 0.0000), 1.0), | ||
((0.1234, 0.9876), (0.1234, 0.9876), 2.0), | ||
((0.1234, 0.9876), (0.8765, 0.2345), 0.5), | ||
((0.1234, 0.9876), (0.1234, 0.9876), 0.0)] | ||
|
||
for p1, p2, r in tries.items: | ||
echo "Through points:" | ||
echo " ", p1 | ||
echo " ", p2 | ||
echo " and radius ", r | ||
echo "You can construct the following circles:" | ||
try: | ||
let (c1, c2) = circles(p1, p2, r) | ||
echo " ", c1 | ||
echo " ", c2 | ||
except EInvalidValue: | ||
echo " ERROR: ", getCurrentExceptionMsg() | ||
echo "" |
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,58 @@ | ||
import queues, sequtils | ||
|
||
type | ||
Node[T] = ref TNode[T] | ||
TNode[T] = object | ||
data: T | ||
left, right: Node[T] | ||
|
||
proc newNode[T](data: T; left, right: Node[T] = nil): Node[T] = | ||
Node[T](data: data, left: left, right: right) | ||
|
||
proc preorder[T](n: Node[T]): seq[T] = | ||
if n == nil: @[] | ||
else: @[n.data] & preorder(n.left) & preorder(n.right) | ||
|
||
proc inorder[T](n: Node[T]): seq[T] = | ||
if n == nil: @[] | ||
else: inorder(n.left) & @[n.data] & inorder(n.right) | ||
|
||
proc postorder[T](n: Node[T]): seq[T] = | ||
if n == nil: @[] | ||
else: postorder(n.left) & postorder(n.right) & @[n.data] | ||
|
||
proc levelorder[T](n: Node[T]): seq[T] = | ||
result = @[] | ||
var queue = initQueue[Node[T]]() | ||
queue.enqueue(n) | ||
while queue.len > 0: | ||
let next = queue.dequeue() | ||
result.add next.data | ||
if next.left != nil: queue.enqueue(next.left) | ||
if next.right != nil: queue.enqueue(next.right) | ||
|
||
var tree = 1.newNode( | ||
2.newNode( | ||
4.newNode( | ||
7.newNode), | ||
5.newNode), | ||
3.newNode( | ||
6.newNode( | ||
8.newNode, | ||
9.newNode))) | ||
|
||
var tree2 = deepCopy tree # not implemented yet, TODO: update then | ||
tree2.data = 10 | ||
tree2.left.data = 20 | ||
tree2.right.left.data = 90 | ||
|
||
echo preorder tree | ||
echo inorder tree | ||
echo postorder tree | ||
echo levelorder tree | ||
|
||
echo "Tree2:" | ||
echo preorder tree2 | ||
echo inorder tree2 | ||
echo postorder tree2 | ||
echo levelorder tree2 |
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,72 @@ | ||
import unsigned | ||
|
||
var | ||
i32: int32 | ||
i64: int64 | ||
u32: uint32 | ||
u64: uint64 | ||
|
||
echo "Signed 32-bit:" | ||
|
||
i32 = -2147483647 | ||
echo(-(i32 - 1)) | ||
|
||
i32 = 2000000000 | ||
echo(i32 + i32) | ||
|
||
i32 = -2147483647 | ||
echo(i32 + i32) | ||
|
||
i32 = 46341 | ||
echo(i32 * i32) | ||
|
||
i32 = -2147483647 | ||
echo((i32-1) div -1) | ||
|
||
echo "Signed 64-bit:" | ||
|
||
i64 = -9223372036854775807 | ||
echo(-(i64 - 1)) | ||
|
||
i64 = 5000000000000000000 | ||
echo(i64 + i64) | ||
|
||
i64 = -9223372036854775807 | ||
echo(i64 + i64) | ||
|
||
i64 = 3037000500 | ||
echo(i64 * i64) | ||
|
||
i64 = -9223372036854775807 | ||
echo((i64-1) div -1) | ||
|
||
echo "Unsigned 32-bit:" | ||
|
||
#u32 = -4294967295 # Compile time error | ||
#u32 = 4294967295'u32 # TODO: This should work without the literal | ||
#echo(-u32) # Compile time error | ||
|
||
u32 = 3000000000'u32 # TODO: This should work without the literal | ||
echo(u32 + u32) | ||
|
||
var a: uint32 = 2147483647 | ||
u32 = 4294967295'u32 | ||
echo(a - u32) | ||
|
||
u32 = 65537 | ||
echo(u32 * u32) | ||
|
||
echo "Unsigned 64-bit:" | ||
|
||
#u64 = 18446744073709551615'u64 # TODO: This should work | ||
#echo(-u64) | ||
|
||
#u64 = 10000000000000000000'u64 # TODO: This should work | ||
#echo(u64 + u64) | ||
|
||
#var aa: uint64 = 9223372036854775807'u64 # TODO: This should work without the literal | ||
#u64 = 18446744073709551615'u64 # TODO: This should work | ||
#echo(aa - u64) | ||
|
||
u64 = 4294967296'u64 # This should work without the literal | ||
echo(u64 * u64) |
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 @@ | ||
import strutils | ||
|
||
proc count(s: string, sub: char): int = | ||
var i = 0 | ||
while true: | ||
i = s.find(sub, i) | ||
if i < 0: | ||
break | ||
inc i | ||
inc result | ||
|
||
proc popcount(n): int = n.toBin(64).count('1') | ||
|
||
const primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61} | ||
|
||
var p = newSeq[int]() | ||
var i = 0 | ||
while p.len < 25: | ||
if popcount(i) in primes: p.add i | ||
inc i | ||
|
||
echo p | ||
|
||
p = @[] | ||
i = 888_888_877 | ||
while i <= 888_888_888: | ||
if popcount(i) in primes: p.add i | ||
inc i | ||
|
||
echo p |
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