Home Prime Time All The Time
Post
Cancel

Prime Time All The Time

Prime numbers

A prime number is a number divisable by 1 and itself. That’s it! There’s nothing more to it. There might be if you want it to be, but that’s the basic of prime numbers. “How come people often talk about them”, the simpleton asks. Fear not, allow the intellectual to answer.

Mr Bean Waiting

It seems the intellectual did not respond. Allow me to take a stab at it.

But all of that yapping is simply to get to the actual content of the post. I want to see in how many programming languages can I write an isPrime function! With that knowledge, welcome to the abyss as we dive into all sorts of programming languages trying to discover a prime!

Criteria

Each language must display the output of some isPrime function with an input value of 911. For most languages the output will be:

1
isPrime(911) = True

Later on I want to link a website where the specific piece of code can be executed directly. It should be a copy and paste and work as is!

Table Of Contents

General-Purpose


Java

1
2
3
4
5
6
7
8
9
boolean isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

System.out.println("isPrime(911) = " + isPrime(911))

Groovy

1
2
3
4
5
6
7
8
9
boolean isPrime(int n) {
    if (n < 2) return false
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false
    }
    return true
}

println "isPrime(911) = " + isPrime(911)

Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
fun isPrime(n: Int): Boolean {
    if (n < 2) return false

    var i = 2
    while (i * i <= n) {
        if (n % i == 0) return false
        i++
    }

    return true
}

println("isPrime(911) = " + isPrime(911))

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
func isPrime(n int) bool {
    if n < 2 {
        return false
    }

    for i := 2; i*i <= n; i++ {
        if n%i == 0 {
            return false
        }
    }

    return true
}

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
def isPrime(n):
    if n < 2:
        return False
    
    i = 2
    while(i * i <= n):
        if (n % i == 0):
            return False
        i += 1
    
    return True

print(f'isPrime(911) = {isPrime(911)}')

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int isPrime(int n) {
    if (n < 2) {
        return 0;
    }
    
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return 0;
        }
    }
    
    return 1;
}

printf("isPrime(911) = %d", isPrime(911));

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int isPrime(int n) {
    if (n < 2) {
        return 0;
    }
    
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return 0;
        }
    }
    
    return 1;
}

std::cout << "isPrime(911) = " << isPrime(911);

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static bool isPrime(int n)
    {
        if (n < 2)
        {
            return false;
        }
        
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        
        return true;
    }

Console.WriteLine ("isPrime(911) = " + isPrime(911));

Swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func isPrime(n: Int) -> Bool {
    if (n < 2) {
        return false
    }

    var i = 2
    while (i * i <= n) {
        if (n % i == 0) {
            return false
        }
        i += 1
    }

    return true
}

print("isPrime(n: 911) =", isPrime(n: 911))

Rust

To do…

Dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isPrime(int n) {
  if (n < 2) {
    return false;
  }
  
  for (int i = 2; i * i < n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  
  return true;
}

print('isPrime(911) = ${isPrime(911)}');

Ruby

To do…

Vala

To do…

Julia

To do…

Nim

To do…

Perl

To do…

PHP

To do…

Scala

To do…

Web Development & Frameworks


JavaScript

To do…

TypeScript

To do…

HTML/CSS

To do…

Razor

To do…

ASP.NET

To do…

Svelte

To do…

Hack

To do…

Mobile & UI Development


Flutter

To do…

React Native

To do…

Xamarin

To do…

Objective-C

To do…

Scientific & Mathematical Computing


R

To do…

MATLAB

To do…

SAS

To do…

Octave

To do…

Wolfram

To do…

Stan

To do…

JAX

To do…

Maple

To do…

Maxima

To do…

SageMath

To do…

J

To do…

Game Development & Graphics


UnityScript

To do…

UnrealScript

To do…

GDScript

To do…

Haxe

To do…

Löve2D

To do…

GameMaker

To do…

Cocos2d

To do…

Embedded Systems & Low-Level


Assembly

To do…

Embedded C

To do…

Ada

To do…

Forth

To do…

MicroPython

To do…

NXC

To do…

VHDL

To do…

Verilog

To do…

PIC Assembly Language

To do…

Zig

To do…

Scripting & Shell


Lua

To do…

Tcl or Tool Command Language

To do…

PowerShell

To do…

Bash

To do…

AWK

To do…

Fish Shell

To do…

BeanShell

To do…

Functional & Logical


Haskell

To do…

F#

To do…

OCaml

To do…

Elm

To do…

Idris

To do…

PureScript

To do…

Scheme

To do…

Lisp

To do…

Agda

To do…

Clojure

To do…

Racket

To do…

Elixir

To do…

Database Query & Blockchain


SQL

To do…

PL/SQL

To do…

T-SQL

To do…

GraphQL

To do…

Cypher

To do…

SPARQL

To do…

Datalog

To do…

Solidity

To do…

Vyper

To do…

Michelson for Tezos Blockchain

To do…

Simplicity

To do…

Move for Diem Blockchain

To do…

Hoon

To do…

Clarity on Stacks blockchain

To do…

Performance & Parallel Computing

FORTRAN

To do…

OpenCL

To do…

CUDA C/C++

To do…

Chapel

To do…

Futhark

To do…

Pony

To do…

Crystal

To do…

Red

To do…

Q

To do…

This post is licensed under CC BY 4.0 by the author.