How to resolve the algorithm Amicable pairs step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amicable pairs step by step in the FutureBasic programming language

Table of Contents

Problem Statement

Two integers

N

{\displaystyle N}

and

M

{\displaystyle M}

are said to be amicable pairs if

N ≠ M

{\displaystyle N\neq M}

and the sum of the proper divisors of

N

{\displaystyle N}

(

s u m

(

p r o p D i v s

( N ) )

{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}

)

= M

{\displaystyle =M}

as well as

s u m

(

p r o p D i v s

( M ) )

N

{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}

.

1184 and 1210 are an amicable pair, with proper divisors:

Calculate and show here the Amicable pairs below 20,000; (there are eight).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Amicable pairs step by step in the FutureBasic programming language

Source code in the futurebasic programming language

local fn Sigma( n as long ) as long
  long i, root, sum = 1
  
  if n == 1 then exit fn = 0
  root = sqr(n)
  for i = 2 to root
    if ( n mod i == 0 ) then sum += i + n/i
  next
  if root * root == n then sum -= root
end fn = sum

void local fn CalculateAmicablePairs( limit as long )
  long i, m
  
  printf @"\nAmicable pairs through %ld are:\n", limit
  for i = 2 to limit
    m = fn Sigma(i)
    if ( m > i )
      if ( fn Sigma(m) == i ) then printf @"%6ld and %ld", i, m
    end if
  next
end fn

CFTimeInterval t
t = fn CACurrentMediaTime
fn CalculateAmicablePairs( 20000 )
printf @"\nCompute time: %.3f ms",(fn CACurrentMediaTime-t)*1000

HandleEvents

  

You may also check:How to resolve the algorithm Integer sequence step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Action! programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Julia programming language
You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Objeck programming language