How to resolve the algorithm Arithmetic/Rational step by step in the Fermat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Rational step by step in the Fermat programming language

Table of Contents

Problem Statement

Create a reasonably complete implementation of rational arithmetic in the particular language using the idioms of the language.

Define a new type called frac with binary operator "//" of two integers that returns a structure made up of the numerator and the denominator (as per a rational number). Further define the appropriate rational unary operators abs and '-', with the binary operators for addition '+', subtraction '-', multiplication '×', division '/', integer division '÷', modulo division, the comparison operators (e.g. '<', '≤', '>', & '≥') and equality operators (e.g. '=' & '≠'). Define standard coercion operators for casting int to frac etc. If space allows, define standard increment and decrement operators (e.g. '+:=' & '-:=' etc.). Finally test the operators: Use the new type frac to find all perfect numbers less than 219 by summing the reciprocal of the factors.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic/Rational step by step in the Fermat programming language

Source code in the fermat programming language

for n=2 to 2^19 by 2 do
    s:=3/n;
    m:=1;
    while m<=n/3 do
        if Divides(m,n) then s:=s+1/m; fi;
        m:=m+1;
    od;
    if s=2 then !!n fi;
od;

  

You may also check:How to resolve the algorithm Boolean values step by step in the Idris programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Kotlin programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Haskell programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Clean programming language