6 kyu

Is a number prime?

43,449 of 116,895arithmetric

Description:

Define a function that takes an integer argument and returns a logical value true or false depending on if the integer is a prime.

Per Wikipedia, a prime number ( or a prime ) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Requirements

  • You can assume you will be given an integer input.
  • You can not assume that the integer will be only positive. You may be given negative numbers as well ( or 0 ).
  • NOTE on performance: There are no fancy optimizations required, but still the most trivial solutions might time out. Numbers go up to 2^31 ( or similar, depending on language ). Looping all the way up to n, or n/2, will be too slow.

Example

is_prime(1)  /* false */
is_prime(2)  /* true  */
is_prime(-1) /* false */
mov edi, 1
call is_prime    ; EAX <- 0 (false)

mov edi, 2
call is_prime    ; EAX <- 1 (true)

mov edi, -1
call is_prime    ; EAX <- 0 (false)
bool isPrime(5) = return true
bool isPrime(5) = return true
IsPrime(1) = false
IsPrime(2) = true
IsPrime(-1) = false
is_prime(1)  # 0
is_prime(2)  # 1
is_prime(-1) # 0
is-prime 1 -> False
is-prime 2 -> True
Mathematics
Algorithms

Stats:

CreatedOct 19, 2013
PublishedOct 19, 2013
Warriors Trained249688
Total Skips33946
Total Code Submissions653265
Total Times Completed116895
JavaScript Completions43449
Python Completions34974
Ruby Completions4043
Haskell Completions1079
R Completions256
C# Completions5831
PHP Completions2925
C++ Completions8977
NASM Completions53
Java Completions10558
C Completions4541
Rust Completions1492
COBOL Completions22
Pascal Completions30
λ Calculus Completions4
Go Completions1383
Perl Completions31
D Completions10
Total Stars2573
% of votes with a positive feedback rating84% of 8949
Total "Very Satisfied" Votes6490
Total "Somewhat Satisfied" Votes2054
Total "Not Satisfied" Votes405
Ad
Contributors
  • arithmetric Avatar
  • jhoffner Avatar
  • xcthulhu Avatar
  • richardhsu Avatar
  • Azuaron Avatar
  • bkaes Avatar
  • Unnamed Avatar
  • donaldsebleung Avatar
  • Chrono79 Avatar
  • imjasonmiller Avatar
  • JohanWiltink Avatar
  • B1ts Avatar
  • Blind4Basics Avatar
  • acjoker Avatar
  • Voile Avatar
  • Souzooka Avatar
  • mentalplex Avatar
  • Darshan97 Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • sazzadshopno Avatar
  • hobovsky Avatar
  • studo Avatar
  • uniapi Avatar
  • tonylicoding Avatar
  • username0 Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • Glenn0-0 Avatar
Ad