Foundations 8
- Sum array
- Maximum
- Count even
- Reverse
- Factorial
- Fibonacci
- Euclidean GCD
- Prime check
Verified learner corpus
Eight representative programs from the 30-exercise S1 corpus. Every example has deterministic input and expected output.
Counted input, a bounded loop, and scalar accumulation.
~ rite
$n = read_int()
$sum = 0
~ loop $n
$sum = $sum + read_int()
~ end
!say $sum
~ end5
3 -2 7 10 422naux run solution.nx < input.txtTrial division terminates at the integer square-root boundary.
~ rite
$n = read_int()
$prime = $n >= 2
$divisor = 2
~ while $prime && $divisor * $divisor <= $n
~ if $n % $divisor == 0
$prime = false
~ end
$divisor = $divisor + 1
~ end
!say $prime
~ end97truenaux run solution.nx < input.txtSearch a sorted learner-built collection without a host search builtin.
~ rite
$n = read_int()
$values = []
~ loop $n
$values = queue_push($values, read_int())
~ end
$target = read_int()
$left = 0
$right = $n - 1
$answer = -1
~ while $left <= $right && $answer == -1
$span = $right - $left
$middle = $left + ($span - ($span % 2)) / 2
~ if $values[$middle] == $target
$answer = $middle
~ else
~ if $values[$middle] < $target
$left = $middle + 1
~ else
$right = $middle - 1
~ end
~ end
~ end
!say $answer
~ end7
1 3 5 7 9 11 13
94naux run solution.nx < input.txtMutable indexing, nested loops, and an explicit swap.
~ rite
$n = read_int()
$values = []
~ loop $n
$values = queue_push($values, read_int())
~ end
$pass = 0
~ while $pass < $n
$i = 0
~ while $i + 1 < $n - $pass
~ if $values[$i] > $values[$i + 1]
$temporary = $values[$i]
$values[$i] = $values[$i + 1]
$values[$i + 1] = $temporary
~ end
$i = $i + 1
~ end
$pass = $pass + 1
~ end
$i = 0
~ loop $n
!say $values[$i]
$i = $i + 1
~ end
~ end5
5 1 4 2 81
2
4
5
8naux run solution.nx < input.txtAn adjacency matrix, explicit queue state, and visited tracking.
~ rite
$n = read_int()
$start = read_int()
$adjacency = []
~ loop $n * $n
$adjacency = queue_push($adjacency, read_int())
~ end
$seen = []
$queue = []
~ loop $n
$seen = queue_push($seen, 0)
$queue = queue_push($queue, 0)
~ end
$head = 0
$tail = 1
$queue[0] = $start
$seen[$start] = 1
~ while $head < $tail
$node = $queue[$head]
$head = $head + 1
!say $node
$next = 0
~ while $next < $n
~ if $adjacency[$node * $n + $next] != 0 && $seen[$next] == 0
$seen[$next] = 1
$queue[$tail] = $next
$tail = $tail + 1
~ end
$next = $next + 1
~ end
~ end
~ end5 0
0 1 1 0 0
1 0 0 1 0
1 0 0 1 1
0 1 1 0 0
0 0 1 0 00
1
2
3
4naux run solution.nx < input.txtA descending denomination set and repeated greedy selection.
~ rite
$count = read_int()
$coins = []
~ loop $count
$coins = queue_push($coins, read_int())
~ end
$amount = read_int()
$used = 0
$i = 0
~ while $i < $count
~ while $amount >= $coins[$i]
$amount = $amount - $coins[$i]
$used = $used + 1
~ end
$i = $i + 1
~ end
!say $used
~ end4
25 10 5 1
876naux run solution.nx < input.txtOne-dimensional descending-capacity dynamic programming.
~ rite
$n = read_int()
$capacity = read_int()
$weights = []
$values = []
~ loop $n
$weights = queue_push($weights, read_int())
~ end
~ loop $n
$values = queue_push($values, read_int())
~ end
$best = []
~ loop $capacity + 1
$best = queue_push($best, 0)
~ end
$item = 0
~ while $item < $n
$space = $capacity
~ while $space >= $weights[$item]
$candidate = $best[$space - $weights[$item]] + $values[$item]
~ if $candidate > $best[$space]
$best[$space] = $candidate
~ end
$space = $space - 1
~ end
$item = $item + 1
~ end
!say $best[$capacity]
~ end4 5
2 3 4 5
3 4 5 67naux run solution.nx < input.txtA quadratic learner implementation with explicit state.
~ rite
$n = read_int()
$values = []
$length = []
~ loop $n
$values = queue_push($values, read_int())
$length = queue_push($length, 1)
~ end
$answer = 0
$i = 0
~ while $i < $n
$j = 0
~ while $j < $i
~ if $values[$j] < $values[$i]
$candidate = $length[$j] + 1
~ if $candidate > $length[$i]
$length[$i] = $candidate
~ end
~ end
$j = $j + 1
~ end
~ if $length[$i] > $answer
$answer = $length[$i]
~ end
$i = $i + 1
~ end
!say $answer
~ end8
10 9 2 5 3 7 101 184naux run solution.nx < input.txtComplete S1 inventory