re: Daily Challenge Post #20 - Number Check VIEW POST

FULL DISCUSSION
 

Well, as this isn't the most challenging challenge, I decided to challenge myself ;)

Here is an answer in the stack-based programming language Factor.

! is an integer even?
: even? ( int -- ? )
    2 mod 0 = ;

! is an integer a multiple of ten?
: multiple-of-ten? ( int -- ? )
    10 mod 0 = ;

! helper word (that's a 'function' in factor) for prime? 
: (prime?) ( int m -- ? )
    2dup <= [ t 2nip ] [ 
        2dup mod 0 = [ f 2nip ] [ 1 + (prime?) ] if
    ] if ;

! is an integer prime?
: prime? ( int -- ? )
    2 (prime?)

! all three tests applied to a single integer, output as an array
: all-three ( int -- seq )
    2dup
    [ prime? ] [ even? ] [ multiple-of-ten? ]
    tri* 3array ;


5 all-three
!
! --- Data stack:
! { t f f }
clear
10 all-three
!
! --- Data stack:
! { f t t }

I've never tried anything like this before, so I'd be interested in some feedback if any is available.

Conversely, if anyone would like me to explain what on earth is going on above, please ask and I'll do my best. I really enjoyed writing it.

code of conduct - report abuse