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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Rational step by step in the ERRE 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 ERRE programming language

Source code in the erre programming language

PROGRAM RATIONAL_ARITH

!
! for rosettacode.org
!

TYPE RATIONAL=(NUM,DEN)

DIM SUM:RATIONAL,ONE:RATIONAL,KF:RATIONAL

DIM A:RATIONAL,B:RATIONAL
PROCEDURE ABS(A.->A.)
      A.NUM=ABS(A.NUM)
END PROCEDURE

PROCEDURE NEG(A.->A.)
      A.NUM=-A.NUM
END PROCEDURE

PROCEDURE ADD(A.,B.->A.)
      LOCAL T
      T=A.DEN*B.DEN
      A.NUM=A.NUM*B.DEN+B.NUM*A.DEN
      A.DEN=T
END PROCEDURE

PROCEDURE SUB(A.,B.->A.)
      LOCAL T
      T=A.DEN*B.DEN
      A.NUM=A.NUM*B.DEN-B.NUM*A.DEN
      A.DEN=T
END PROCEDURE

PROCEDURE MULT(A.,B.->A.)
      A.NUM*=B.NUM  A.DEN*=B.DEN
END PROCEDURE

PROCEDURE DIVIDE(A.,B.->A.)
      A.NUM*=B.DEN
      A.DEN*=B.NUM
END PROCEDURE

PROCEDURE EQ(A.,B.->RES%)
      RES%=A.NUM*B.DEN=B.NUM*A.DEN
END PROCEDURE

PROCEDURE LT(A.,B.->RES%)
      RES%=A.NUM*B.DEN
END PROCEDURE

PROCEDURE GT(A.,B.->RES%)
      RES%=A.NUM*B.DEN>B.NUM*A.DEN
END PROCEDURE

PROCEDURE NE(A.,B.->RES%)
      RES%=A.NUM*B.DEN<>B.NUM*A.DEN
END PROCEDURE

PROCEDURE LE(A.,B.->RES%)
      RES%=A.NUM*B.DEN<=B.NUM*A.DEN
END PROCEDURE

PROCEDURE GE(A.,B.->RES%)
      RES%=A.NUM*B.DEN>=B.NUM*A.DEN
END PROCEDURE

PROCEDURE NORMALIZE(A.->A.)
      LOCAL A,B,T
      A=A.NUM   B=A.DEN
      WHILE B<>0 DO
        T=A
        A=B
        B=T-B*INT(T/B)
      END WHILE
      A.NUM/=A  A.DEN/=A
      IF A.DEN<0 THEN A.NUM*=-1 A.DEN*=-1 END IF
END PROCEDURE

BEGIN
    ONE.NUM=1 ONE.DEN=1
    FOR N=2 TO 2^19-1 DO
      SUM.NUM=1 SUM.DEN=N
      FOR K=2 TO SQR(N) DO
        IF N=K*INT(N/K) THEN
          KF.NUM=1 KF.DEN=K
          ADD(SUM.,KF.->SUM.)
          NORMALIZE(SUM.->SUM.)
          KF.DEN=INT(N/K)
          ADD(SUM.,KF.->SUM.)
          NORMALIZE(SUM.->SUM.)
        END IF
      END FOR
      EQ(SUM.,ONE.->RES%)
      IF RES% THEN PRINT(N;" is perfect") END IF
   END FOR
END PROGRAM

  

You may also check:How to resolve the algorithm Jacobi symbol step by step in the 11l programming language
You may also check:How to resolve the algorithm Align columns step by step in the Phix programming language
You may also check:How to resolve the algorithm The sieve of Sundaram step by step in the Racket programming language
You may also check:How to resolve the algorithm Inheritance/Single step by step in the TXR programming language
You may also check:How to resolve the algorithm Algebraic data types step by step in the Phix programming language