How to resolve the algorithm Primality by Wilson's theorem step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by Wilson's theorem step by step in the Forth programming language

Table of Contents

Problem Statement

Write a boolean function that tells whether a given integer is prime using Wilson's theorem. By Wilson's theorem, a number p is prime if and only if p divides (p - 1)! + 1. Remember that 1 and all non-positive integers are not prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by Wilson's theorem step by step in the Forth programming language

Source code in the forth programming language

: fac-mod ( n m -- r )
    >r 1 swap
    begin dup 0> while
        dup rot * r@ mod  swap 1-
    repeat drop rdrop ;

: ?prime ( n -- f )
    dup 1- tuck swap fac-mod = ;

: .primes ( n -- )
    cr 2 ?do i ?prime if i . then loop ;


  

You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the XPL0 programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the C# programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the HolyC programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the zkl programming language
You may also check:How to resolve the algorithm Count in octal step by step in the 8080 Assembly programming language