aboutsummaryrefslogtreecommitdiffstats
path: root/research/experiments/040-against-c/roots.aup
blob: 22c91896ff8650392cd00da9f2d795551b405830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Numbers in a loop (research 124's suite): the square roots of 1 .. n by
# Newton's steps, each until a step stops shrinking, and their sum.
state total = 0
state steps = 0
input go = message {
  n: u32
}
on go {
  let sum = 0
  let count = 0
  for i in 1 .. go.n + 1 {
    let r = i
    for s in 0 .. 64 {
      let next = (r + i / r) / 2
      if next >= r { break }
      r = next
      count = count + 1
    }
    sum = sum + r
  }
  total = sum
  steps = count
}