How to resolve the algorithm Primality by trial division step by step in the MAD programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by trial division step by step in the MAD 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 MAD programming language
Source code in the mad programming language
NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
ENTRY TO PRIME.
WHENEVER N.L.2, FUNCTION RETURN 0B
WHENEVER N.E.N/2*2, FUNCTION RETURN N.E.2
THROUGH TRIAL, FOR FAC=3, 2, FAC*FAC.G.N
TRIAL WHENEVER N.E.N/FAC*FAC, FUNCTION RETURN 0B
FUNCTION RETURN 1B
END OF FUNCTION
PRINT COMMENT $ PRIMES UNDER 100 $
THROUGH CAND, FOR C=0, 1, C.G.100
CAND WHENEVER PRIME.(C), PRINT FORMAT PR,C
VECTOR VALUES PR = $ I3*$
END OF PROGRAM
You may also check:How to resolve the algorithm Polynomial long division step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Faulhaber's formula step by step in the Ruby programming language
You may also check:How to resolve the algorithm Mouse position step by step in the xTalk programming language
You may also check:How to resolve the algorithm Safe primes and unsafe primes step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Common Lisp programming language