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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by trial division step by step in the Oforth 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 Oforth programming language

Source code in the oforth programming language

Integer method: isPrime
| i |
   self 1 <= ifTrue: [ false return ]
   self 3 <= ifTrue: [ true return ]
   self isEven ifTrue: [ false return ]
   3 self sqrt asInteger for: i [ self i mod ifzero: [ false return ] ]
   true ;

  

You may also check:How to resolve the algorithm Time a function step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Jsish programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Cartesian product of two or more lists step by step in the BASIC programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the 11l programming language