-
Notifications
You must be signed in to change notification settings - Fork 1
/
p66.ct
77 lines (62 loc) · 1.66 KB
/
p66.ct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
alias string = [int];
extern printstr: fn(string): unit;
extern printint: fn(int): unit;
extern setindex: fn([int],int,int): unit;
let isqrt_go = fn(a: int, s: int): int {
let a_ = (a + s / a) / 2;
if a_ >= a {
a
} else {
isqrt_go(a_,s)
}
};
let isqrt = fn(a: int): int {
isqrt_go(a,a)
};
let gcd = fn(a: int,b:int): int {
if b > a {
gcd(b,a)
} else {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
};
let fraction_root_go = fn(cur_ind: int, list: [int], root_value: int,
numerator_plus: int, divisor: int, stop_at: int,
len: int): int {
let an = (isqrt(root_value) + numerator_plus) / 2;
if an == stop_at {
cur_ind - 1
} else {
if cur_ind >= len {
printstr("value won't be correct becasue not enough array space was allocated");
0
} else {
setindex(list,cur_ind,an);
let denom = (root_value -
numerator_plus * numerator_plus -
an * an * divisor * divisor +
2 * an * divisor * numerator_plus);
let gcd = gcd(divisor,denom);
}
}
};
let fraction_root = fn(a: int, list: [int], len: int): int {
let a_zero = isqrt(a);
setindex(list,0,a_zero);
if (a_zero * a_zero == a) {
0
} else {
fraction_root_go(1,list,a,a_zero,(a - a_zero * a_zero), a_zero * 2, len)
}
};
let main = fn() {
printint(isqrt(10));
printint(isqrt(100));
printint(isqrt(4));
printint(isqrt(201));
printint(gcd(48,12));
};