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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amicable pairs step by step in the Futhark 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 Futhark programming language

Source code in the futhark programming language

fun divisors(n: int): []int =
  filter (fn x => n%x == 0) (map (1+) (iota (n/2)))

fun amicable((n: int, nd: int), (m: int, md: int)): bool =
  n < m && nd == m && md == n

fun getPair (divs: [upper](int, int)) (flat_i: int): ((int,int), (int,int)) =
  let i = flat_i / upper
  let j = flat_i % upper
  in unsafe (divs[i], divs[j])

fun main(upper: int): [][2]int =
  let range = map (1+) (iota upper)
  let divs = zip range (map (fn n => reduce (+) 0 (divisors n)) range)
  let amicable = filter amicable (map (getPair divs) (iota (upper*upper)))
  in map (fn (np,mp) => [#1 np, #1 mp]) amicable


  

You may also check:How to resolve the algorithm Real constants and functions step by step in the Lasso programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Wren programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Tcl programming language
You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the C programming language