How to resolve the algorithm Primality by trial division step by step in the Arturo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by trial division step by step in the Arturo programming language

Table of Contents

Problem Statement

Write a boolean function that tells whether a given integer is prime.

Remember that   1   and all non-positive numbers are not prime. Use trial division. Even numbers greater than   2   may be eliminated right away. A loop from   3   to   √ n    will suffice,   but other loops are allowed.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the Arturo programming language

Source code in the arturo programming language

isPrime?: function [n][
    if n=2 -> return true
    if n=3 -> return true
    if or? n=<1 0=n%2 -> return false
 
    high: to :integer sqrt n
    loop high..2 .step: 3 'i [
    	if 0=n%i -> return false
    ]

    return true
]
 
loop 1..20 'i [
    print ["isPrime?" i "=" isPrime? i ]
]


  

You may also check:How to resolve the algorithm Detect division by zero step by step in the Groovy programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the Rust programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the PL/M programming language
You may also check:How to resolve the algorithm String case step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Io programming language