.isPrime()

Description

isPrime returns a boolean indicating whether the input number is prime (a number greater than 1 that is only divisible by 1 and itself).

Parameters

isPrime takes no parameters.

Usage On Numbers

// Basic usage
num = 17
log num.isPrime()
// true
// 17 is prime

num = 16
log num.isPrime()
// false
// 16 is not prime (divisible by 1, 2, 4, 8, 16)

// Works with larger numbers
log 997.isPrime()
// true
// 997 is prime

// Special cases
log 1.isPrime()
// false
// 1 is not considered prime by definition

log 2.isPrime()
// true
// 2 is the smallest and only even prime number

// Negative numbers and zero are not prime
log (-7).isPrime()
// false

log 0.isPrime()
// false

Last updated

Was this helpful?