Skip to content

Instantly share code, notes, and snippets.

@mndrix
Created December 9, 2016 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mndrix/a2caeb8703a0b1e3e5f867a7d9f032ba to your computer and use it in GitHub Desktop.
Save mndrix/a2caeb8703a0b1e3e5f867a7d9f032ba to your computer and use it in GitHub Desktop.
Collatz Conjecture in Eve
# Collatz Conjecture
## Description
The Collatz Conjecture deals with an integer #`n` and the #`steps` it takes to reach 1.
```
commit
[#n]
[#step]
```
If #`n` is even, divide it by 2. If #`n` is odd, multiply it by 3 and add 1.
```eve
search
n = [#n value > 1]
m = mod[value: n.value, by: 2]
next = if m = 0 then n.value / 2 else n.value * 3 + 1
commit
n.value := next
[#step from: n.value, to: next]
```
Does #`n` always converge to 1?
## User interface
Let the user change n
```eve
search @browser @event
[#click element: [#button]] // remove me for a bug
[#input name: "n", value: new]
search
n = [#n]
steps = [#step]
commit
n.value := new
steps := none
[#step]
```
by changing this form
```eve
commit @browser
[#label text: "Choose n to start: ", children:
[#input type: "number" name: "n"]]
[#button text: "Go"]
```
Show how many steps were required to converge.
```
search
[#n value: n]
steps = count[given:[#step]] - 1
bind @browser
[#div sort: 1, text: "reached {{n}} in {{steps}} steps"]
```
Show the individual steps taken.
```
search
[#step from to]
bind @browser
[#div sort: 2, text: "{{from}} -> {{to}}"]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment