Hoon101/week5/goldman.hoon
2019-08-08 15:40:11 +12:00

90 lines
2.6 KiB
Plaintext

:: create a gate taking an atom as input, giving it the face 'n'
::
|= n=@
:: compose the calling of `goldbach` with the core containing goldbach
:: this has the effect of `running` the core when the outer gate is run
::
=< (goldbach n)
:: form a core
::
|%
:: create an arm named 'prime'
::
++ prime
:: create a gate with an atom input given the face 'n'
::
|= n=@
:: typecast the output to a flag
::
^- ?
:: if n is less than two, return false, otherwise return the other branch
::
?: (lth n 2) |
:: if n is less than 4, return true, otherwise return the other branch
::
?: (lth n 4) &
:: add the atom named i to the subject, set to 2
::
=/ i=@ 2
:: add the atom named j to the subject, set to 2
::
=/ j=@ 2
:: create a gate and typecast the output to a flag
::
|- ^- ?
:: if i * j equals n, return false, otherwise return the other branch
::
?: =((mul i j) n) |
:: if j is equal to n/2, return true, otherwise return the other branch
::
?: =(j (div n 2)) &
:: if i*j is greater than n, return the first branch, else the second
::
?: (gth (mul i j) n)
:: call the current battery (defined at the gate) with a modified payload
:: of i set to 2, and j incremented by one
::
$(i 2, j +(j))
:: call the current battery with the modified payload of i incremented by one
::
$(i +(i))
:: start a new arm called goldbach (this arm gets called with the tisgal)
::
++ goldbach
:: create a gate with an input atom faced 'n'
::
|= n=@
:: typecast the output as a union of a flag (?)
:: and a cell of a cell of atoms, and a flag ([[@ @] ?])
::
^- ?(? [[@ @] ?])
:: if one of; n is less than 4, or n is odd, is true, return false
:: otherwise return the other branch
::
?: |((lth n 4) =((mod n 2) 1)) |
:: attach an atom name i to the subject, set to 2
::
=/ i=@ 2
:: attach an atom name i to the subject, set to n minus 2
::
=/ j=@ (sub n 2)
:: create a trap, and typecast the output to the aformentioned union of
:: flag and cell of cell of atoms, and flag
::
|- ^- ?(? [[@ @] ?])
:: if both i and j are prime numbers, return a cell of:
:: a cell of i and j, and a false flag
:: otherwise return the other branch
::
?: &((prime i) (prime j)) [[i j] |]
:: if n is equal to i plus 2, return a true flag, otherwise the next branch
::
?: =((add 2 i) n) &
:: run the current battery, with an updated payload of
:: i incremented, and j decremented
::
$(i +(i), j (dec j))
:: close off the core
::
-- :: this is for my pal ~rapfyr-diglyt