Skip to main content
added 5 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Mouse, 6565 47 bytes

?N:1N.=[0!$]N.2=[1!$]N.2\0=[0!$]3I0S:1I:(I.N.<^N=0=^N.I.\0=[0!$]I\0=[S.1+S:]I.1+I:)1S.1=!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
10 N.S: = [            ~ IfStart Na ==summation 1...
variable at 0
1 !I: $           ~ Print 0~ andStart exit
]
2an N.interator =variable [at 1
( I. N. = 0 = ^   ~ If N == 2, printWhile 1I and!= exitN
  1 ! $
]
N. 2I. \ 0 = [      ~ IfCheck 2whether I divides N, print 0 and exit
  0 ! $
]
3S. I:1 + S:     ~ If so, increment the sum
  ~]
 Assign I. =1 3
(+ I.: N. < ^     ~ Increment ~the Whileiterator
)
S. I1 <= N
!  N. I. \ 0 = [   ~ If Ithe dividessum Nis 1, printthe 0only anddivisor exitencountered
    0 ! $
  ]
  I. 1 + I:     ~ is ~1 Increment(we I
)didn't go all the way to N in the
1 ! $             ~ If we've made~ itloop) thisand far,thus N is prime
$

Mouse, 65 bytes

?N:1N.=[0!$]N.2=[1!$]N.2\0=[0!$]3I:(I.N.<^N.I.\0=[0!$]I.1+I:)1!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
1 N. = [          ~ If N == 1...
  0 ! $           ~ Print 0 and exit
]
2 N. = [          ~ If N == 2, print 1 and exit
  1 ! $
]
N. 2 \ 0 = [      ~ If 2 divides N, print 0 and exit
  0 ! $
]
3 I:              ~ Assign I = 3
( I. N. < ^       ~ While I < N
  N. I. \ 0 = [   ~ If I divides N, print 0 and exit
    0 ! $
  ]
  I. 1 + I:       ~ Increment I
)
1 ! $             ~ If we've made it this far, N is prime

Mouse, 65 47 bytes

?N:0S:1I:(I.N.=0=^N.I.\0=[S.1+S:]I.1+I:)S.1=!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
0 S:              ~ Start a summation variable at 0
1 I:              ~ Start an interator variable at 1
( I. N. = 0 = ^   ~ While I != N
  N. I. \ 0 = [   ~ Check whether I divides N
    S. 1 + S:     ~ If so, increment the sum
  ]
  I. 1 + I:       ~ Increment the iterator
)
S. 1 = !          ~ If the sum is 1, the only divisor encountered
                  ~ is 1 (we didn't go all the way to N in the
                  ~ loop) and thus N is prime
$
added 82 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Mouse, 5665 bytes

?N:1N.=[0!$]N.2=[1!$]N.2\0=[0!$]3I:(I.N.<^N.I.\0=[0!$]I.1+I:)1!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
1 N. = [          ~ If N == 1...
  0 ! $           ~ Print 0 and exit
]
2 N. = [          ~ If N == 2, print 1 and exit
  1 ! $
]
N. 2 \ 0 = [      ~ If 2 divides N, print 0 and exit
  0 ! $
]
3 I:              ~ Assign I = 3
( I. N. < ^       ~ While I < N
  N. I. \ 0 = [   ~ If I divides N, print 0 and exit
    0 ! $
  ]
  I. 1 + I:       ~ Increment I
)
1 ! $             ~ If we've made it this far, N is prime

Mouse, 56 bytes

?N:1N.=[0!$]N.2\0=[0!$]3I:(I.N.<^N.I.\0=[0!$]I.1+I:)1!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
1 N. = [          ~ If N == 1...
  0 ! $           ~ Print 0 and exit
]
N. 2 \ 0 = [      ~ If 2 divides N, print 0 and exit
  0 ! $
]
3 I:              ~ Assign I = 3
( I. N. < ^       ~ While I < N
  N. I. \ 0 = [   ~ If I divides N, print 0 and exit
    0 ! $
  ]
  I. 1 + I:       ~ Increment I
)
1 ! $             ~ If we've made it this far, N is prime

Mouse, 65 bytes

?N:1N.=[0!$]N.2=[1!$]N.2\0=[0!$]3I:(I.N.<^N.I.\0=[0!$]I.1+I:)1!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
1 N. = [          ~ If N == 1...
  0 ! $           ~ Print 0 and exit
]
2 N. = [          ~ If N == 2, print 1 and exit
  1 ! $
]
N. 2 \ 0 = [      ~ If 2 divides N, print 0 and exit
  0 ! $
]
3 I:              ~ Assign I = 3
( I. N. < ^       ~ While I < N
  N. I. \ 0 = [   ~ If I divides N, print 0 and exit
    0 ! $
  ]
  I. 1 + I:       ~ Increment I
)
1 ! $             ~ If we've made it this far, N is prime
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Mouse, 56 bytes

?N:1N.=[0!$]N.2\0=[0!$]3I:(I.N.<^N.I.\0=[0!$]I.1+I:)1!$

This uses trial division.

Ungolfed:

? N:              ~ Read an integer from STDIN and store it in N
1 N. = [          ~ If N == 1...
  0 ! $           ~ Print 0 and exit
]
N. 2 \ 0 = [      ~ If 2 divides N, print 0 and exit
  0 ! $
]
3 I:              ~ Assign I = 3
( I. N. < ^       ~ While I < N
  N. I. \ 0 = [   ~ If I divides N, print 0 and exit
    0 ! $
  ]
  I. 1 + I:       ~ Increment I
)
1 ! $             ~ If we've made it this far, N is prime