Amicable pairs: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 21:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F sum_proper_divisors(n)
R I n < 2 {0} E sum((1 .. n I/ 2).filter(it -> (@n % it) == 0))
 
Line 30:
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="8080asm"> org 100h
;;; Calculate proper divisors of 2..20000
lxi h,pdiv + 4 ; 2 bytes per entry
Line 163:
 
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm">LIMIT: equ 20000 ; Maximum value
cpu 8086
org 100h
Line 264:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program amicable64.s */
Line 442:
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:SIEVE.ACT"
 
CARD FUNC SumDivisors(CARD x)
Line 499:
[[http://rosettacode.org/wiki/Proper_divisors#Ada]].
 
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO, Generic_Divisors; use Ada.Text_IO;
procedure Amicable_Pairs is
Line 533:
=={{header|ALGOL 60}}==
{{works with|A60}}
<syntaxhighlight lang="algol60">
begin
 
Line 601:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68"># returns the sum of the proper divisors of n #
# if n = 1, 0 or -1, we return 0 #
PROC sum proper divisors = ( INT n )INT:
Line 660:
{{Trans|GFA Basic}}
 
<syntaxhighlight lang=ANSI"ansi Standardstandard BASICbasic">100 DECLARE EXTERNAL FUNCTION sum_proper_divisors
110 CLEAR
120 !
Line 704:
{{Trans|JavaScript}}
 
<syntaxhighlight lang=AppleScript"applescript">-- AMICABLE PAIRS ------------------------------------------------------------
 
-- amicablePairsUpTo :: Int -> Int
Line 848:
end mReturn</syntaxhighlight>
{{Out}}
<syntaxhighlight lang=AppleScript"applescript">{{220, 284}, {1184, 1210}, {2620, 2924}, {5020, 5564},
{6232, 6368}, {10744, 10856}, {12285, 14595}, {17296, 18416}}</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI or android with termux */
/* program amicable.s */
Line 1,016:
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">properDivs: function [x] ->
(factors x) -- x
 
Line 1,042:
 
=={{header|ATS}}==
<syntaxhighlight lang=ATS"ats">
(* ****** ****** *)
//
Line 1,162:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="d">SetBatchLines -1
Loop, 20000
{
Line 1,228:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
#!/bin/awk -f
function sumprop(num, i,sum,root) {
Line 1,274:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=BASIC256"basic256">function SumProperDivisors(number)
if number < 2 then return 0
sum = 0
Line 1,313:
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
manifest $(
Line 1,356:
=={{header|Befunge}}==
 
<syntaxhighlight lang="befunge">v_@#-*8*:"2":$_:#!2#*8#g*#6:#0*#!:#-*#<v>*/.55+,
1>$$:28*:*:*%\28*:*:*/`06p28*:*:*/\2v %%^:*:<>*v
+|!:-1g60/*:*:*82::+**:*:<<>:#**#8:#<*^>.28*^8 :
Line 1,378:
 
The program will overflow and error in all sorts of ways when given a commandline argument >= UINT_MAX/2 (generally 2^31)
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 1,462:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,512:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <vector>
#include <unordered_map>
Line 1,576:
 
=={{header|Clojure}}==
<syntaxhighlight lang="lisp">
(ns example
(:gen-class))
Line 1,614:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Generate proper divisors from 1 to max
proper_divisors = proc (max: int) returns (array[int])
divs: array[int] := array[int]$fill(1, max, 0)
Line 1,655:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(let ((cache (make-hash-table)))
(defun sum-proper-divisors (n)
(or (gethash n cache)
Line 1,675:
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
const LIMIT := 20000;
Line 1,726:
 
=={{header|Crystal}}==
<syntaxhighlight lang=Crystal"crystal">
MX = 524_000_000
N = Math.sqrt(MX).to_u32
Line 1,767:
=={{header|D}}==
{{trans|Python}}
<syntaxhighlight lang="d">void main() @safe /*@nogc*/ {
import std.stdio, std.algorithm, std.range, std.typecons, std.array;
 
Line 1,811:
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Fill a given array such that for each N,
* P[n] is the sum of proper divisors of N */
proc nonrec propdivs([*] word p) void:
Line 1,850:
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
;; using (sum-divisors) from math.lib
 
Line 1,873:
=={{header|Ela}}==
{{trans|Haskell}}
<syntaxhighlight lang="ela">open monad io number list
 
divisors n = filter ((0 ==) << (n `mod`)) [1..(n `div` 2)]
Line 1,889:
{{trans|C#}}
ELENA 5.0 :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,937:
</pre>
=== Alternative variant using strong-typed closures ===
<syntaxhighlight lang="elena">import extensions;
import system'routines'stex;
import system'collections;
Line 1,972:
}</syntaxhighlight>
=== Alternative variant using yield enumerator ===
<syntaxhighlight lang="elena">import extensions;
import system'routines'stex;
import system'collections;
Line 2,032:
{{works with|Elixir|1.2}}
With [[proper_divisors#Elixir]] in place:
<syntaxhighlight lang="elixir">defmodule Proper do
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,063:
Very slow solution. Same functions by and large as in proper divisors and co.
 
<syntaxhighlight lang="erlang">
-module(properdivs).
-export([amicable/1,divs/1,sumdivs/1]).
Line 2,135:
[See the talk section &nbsp; '''amicable pairs, out of order''' &nbsp; for this Rosetta Code task.]
 
<syntaxhighlight lang="erlang">
friendly(Limit) ->
List = [{X,properdivs:sumdivs(X)} || X <- lists:seq(3,Limit)],
Line 2,154:
 
We might answer a challenge by saying:
<syntaxhighlight lang="erlang">
friendly(Limit) ->
List = [{X,properdivs:sumdivs(X)} || X <- lists:seq(3,Limit)],
Line 2,192:
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">PROGRAM AMICABLE
 
CONST LIMIT=20000
Line 2,233:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
[2..20000 - 1]
|> List.map (fun n-> n, ([1..n/2] |> List.filter (fun x->n % x = 0) |> List.sum))
Line 2,258:
This solution focuses on the language's namesake: factoring code into small words which are subsequently composed to form more powerful — yet just as simple — words. Using this approach, the final word naturally arrives at the solution. This is often referred to as the bottom-up approach, which is a way in which Factor (and other concatenative languages) commonly differs from other languages.
 
<syntaxhighlight lang=Factor"factor">
USING: grouping math.primes.factors math.ranges ;
 
Line 2,295:
Calculate many times the divisors.
 
<syntaxhighlight lang="forth">: proper-divisors ( n -- 1..n )
dup 2 / 1+ 1 ?do
dup i mod 0= if i swap then
Line 2,333:
Use memory to store sum of divisors, a little quicker.
 
<syntaxhighlight lang="forth">variable amicable-table
 
: proper-divisors ( n -- 1..n )
Line 2,394:
Amicable! 17296 18416
 
<syntaxhighlight lang=FORTRAN"fortran">
MODULE FACTORSTUFF !This protocol evades the need for multiple parameters, or COMMON, or one shapeless main line...
Concocted by R.N.McLean, MMXV.
Line 2,465:
=={{header|FreeBASIC}}==
===using Mod===
<syntaxhighlight lang="freebasic">
' FreeBASIC v1.05.0 win64
 
Line 2,517:
</pre>
===using "Sieve of Erathosthenes" style===
<syntaxhighlight lang="freebasic">' version 04-10-2016
' compile with: fbc -s console
' replaced the function with 2 FOR NEXT loops
Line 2,569:
=={{header|Frink}}==
This example uses Frink's built-in efficient factorization algorithms. It can work for arbitrarily large numbers.
<syntaxhighlight lang="frink">
n = 1
seen = new set
Line 2,605:
This program is much too parallel and manifests all the pairs, which requires a giant amount of memory.
 
<syntaxhighlight lang="text">
fun divisors(n: int): []int =
filter (fn x => n%x == 0) (map (1+) (iota (n/2)))
Line 2,626:
=={{header|GFA Basic}}==
 
<syntaxhighlight lang="text">
OPENW 1
CLEARW 1
Line 2,685:
 
=={{header|Go}}==
<syntaxhighlight lang=Go"go">package main
 
import "fmt"
Line 2,727:
 
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell"haskell">divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
 
Line 2,743:
Or, deriving proper divisors above the square root as cofactors (for better performance)
 
<syntaxhighlight lang=Haskell"haskell">import Data.Bool (bool)
 
amicablePairsUpTo :: Int -> [(Int, Int)]
Line 2,778:
[[Proper divisors#J|Proper Divisor implementation]]:
 
<syntaxhighlight lang=J"j">factors=: [: /:~@, */&>@{@((^ i.@>:)&.>/)@q:~&__
properDivisors=: factors -. -.&1</syntaxhighlight>
 
Amicable pairs:
 
<syntaxhighlight lang=J"j"> 1 + 0 20000 #: I. ,(</~@i.@# * (* |:))(=/ +/@properDivisors@>) 1 + i.20000
220 284
1184 1210
Line 2,795:
=={{header|Java}}==
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Line 2,836:
===ES5===
 
<syntaxhighlight lang=JavaScript"javascript">(function (max) {
// Proper divisors
Line 2,921:
|}
 
<syntaxhighlight lang=JavaScript"javascript">[[220,284],[1184,1210],[2620,2924],[5020,5564],
[6232,6368],[10744,10856],[12285,14595],[17296,18416]]</syntaxhighlight>
 
===ES6===
 
<syntaxhighlight lang=JavaScript"javascript">(() => {
'use strict';
 
Line 2,998:
 
=={{header|jq}}==
<syntaxhighlight lang="jq"># unordered
def proper_divisors:
. as $n
Line 3,025:
task(20000)</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">$ jq -c -n -f amicable_pairs.jq
220 and 284 are amicable
1184 and 1210 are amicable
Line 3,038:
Given <code>factor</code>, it is not necessary to calculate the individual divisors to compute their sum. See [[Abundant,_deficient_and_perfect_number_classifications#Julia|Abundant, deficient and perfect number classifications]] for the details.
It is safe to exclude primes from consideration; their proper divisor sum is always 1. This code also uses a minor trick to ensure that none of the numbers identified are above the limit. All numbers in the range are checked for an amicable partner, but the pair is cataloged only when the greater member is reached.
<syntaxhighlight lang=Julia"julia">using Primes, Printf
 
function pcontrib(p::Int64, a::Int64)
Line 3,218:
 
=={{header|K}}==
<syntaxhighlight lang="k">
propdivs:{1+&0=x!'1+!x%2}
(8,2)#v@&{(x=+/propdivs[a])&~x=a:+/propdivs[x]}' v:1+!20000
Line 3,232:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1
 
fun sumProperDivisors(n: Int): Int {
Line 3,267:
0.02 of a second in 16 lines of code.
The vital trick is to just set m to the sum of n's proper divisors each time. That way you only have to test the reverse, dividing your run time by half the loop limit (ie. 10,000)!
<syntaxhighlight lang="lua">function sumDivs (n)
local sum = 1
for d = 2, math.sqrt(n) do
Line 3,298:
=={{header|MAD}}==
 
<syntaxhighlight lang=MAD"mad"> NORMAL MODE IS INTEGER
DIMENSION DIVS(20000)
PRINT COMMENT $ AMICABLE PAIRS$
Line 3,342:
{{output?}}
 
<syntaxhighlight lang=Maple"maple">
with(NumberTheory):
pairs:=[];
Line 3,359:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">amicableQ[n_] :=
Module[{sum = Total[Most@Divisors@n]},
sum != n && n == Total[Most@Divisors@sum]]
Line 3,377:
 
=={{header|MATLAB}}==
<syntaxhighlight lang=Matlab"matlab">function amicable
tic
N=2:1:20000; aN=[];
Line 3,433:
Being a novice, I submitted my code to the Nim community for review and received much feedback and advice.
They were instrumental in fine-tuning this code for style and readability, I can't thank them enough.
<syntaxhighlight lang=Nim"nim">
from math import sqrt
 
Line 3,472:
Here's a second version that uses a large amount of memory but runs in 2m32seconds.
Again, thanks to the Nim community
<syntaxhighlight lang=Nim"nim">
from math import sqrt
 
Line 3,509:
 
=={{header|Oberon-2}}==
<syntaxhighlight lang=Oberon2"oberon2">
MODULE AmicablePairs;
IMPORT
Line 3,565:
Using properDivs implementation tasks without optimization (calculating proper divisors sum without returning a list for instance) :
 
<syntaxhighlight lang=Oforth"oforth">import: mapping
 
Integer method: properDivs -- []
Line 3,587:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec isqrt n =
if n = 1 then 1
else let _n = isqrt (n - 1) in
Line 3,617:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">for(x=1,20000,my(y=sigma(x)-x); if(y>x && x == sigma(y)-y,print(x" "y)))</syntaxhighlight>
{{out}}
<pre>220 284
Line 3,650:
Amicable! 17296,18416,
 
Source file:<syntaxhighlight lang="pascal">
Program SumOfFactors; uses crt; {Perpetrated by R.N.McLean, December MCMXCV}
//{$DEFINE ShowOverflow}
Line 3,743:
===More expansive.===
a "normal" Version. Nearly fast as perl using nTheory.
<syntaxhighlight lang="pascal">program AmicablePairs;
{$IFDEF FPC}
{$MODE DELPHI}
Line 4,031:
Using "Sieve of Erathosthenes"-style
 
<syntaxhighlight lang="pascal">program AmicPair;
{find amicable pairs in a limited region 2..MAX
beware that >both< numbers must be smaller than MAX
Line 4,322:
Not particularly clever, but instant for this example, and does up to 20 million in 11 seconds.
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use ntheory qw/divisor_sum/;
for my $x (1..20000) {
my $y = divisor_sum($x)-$x;
Line 4,338:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">20000</span> <span style="color: #008080;">do</span>
Line 4,358:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">def sumDivs
var n
1 var sum n sqrt
Line 4,383:
=={{header|PHP}}==
 
<syntaxhighlight lang="php"><?php
 
function sumDivs ($n) {
Line 4,419:
 
Also, the calculation of the sum of divisors is tabled (the table is cleared between each run).
<syntaxhighlight lang=Picat"picat">go =>
N = 20000,
 
Line 4,529:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de accud (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 4,580:
=={{header|PL/I}}==
{{trans|REXX}}
<syntaxhighlight lang="pli">*process source xref;
ami: Proc Options(main);
p9a=time();
Line 4,654:
==={{header|PL/I-80}}===
Rather than populating an array with the sum of the proper divisors and then searching, the approach here performs an direct test, saving memory, and at a minimal penalty in execution speed, even though about 25% more calls are made to sumf() than would be required simply to initialize an array.
<syntaxhighlight lang=PL"pl/Ii">
amicable: procedure options (main);
 
Line 4,718:
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 4,774:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang=PowerShell"powershell">
function Get-ProperDivisorSum ( [int]$N )
{
Line 4,823:
With some guidance from other solutions here:
 
<syntaxhighlight lang="prolog">divisor(N, Divisor) :-
UpperBound is round(sqrt(N)),
between(1, UpperBound, D),
Line 4,859:
Output:
 
<syntaxhighlight lang="prolog">?- amicable_pairs_under_20000(R).
R = [220-284, 1184-1210, 2620-2924, 5020-5564, 6232-6368, 10744-10856, 12285-14595, 17296-18416].</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">
EnableExplicit
 
Line 4,916:
=={{header|Python}}==
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
<syntaxhighlight lang="python">from proper_divisors import proper_divs
 
def amicable(rangemax=20000):
Line 4,958:
Or, supplying our own '''properDivisors''' function, and defining the harvest in terms of a generic '''concatMap''':
 
<syntaxhighlight lang="python">'''Amicable pairs'''
 
from itertools import chain
Line 5,049:
<code>properdivisors</code> is defined at [[Proper divisors#Quackery]].
 
<syntaxhighlight lang=Quackery"quackery"> [ properdivisors
dup size 0 = iff
[ drop 0 ] done
Line 5,080:
=={{header|R}}==
 
<syntaxhighlight lang=R"r">
divisors <- function (n) {
Filter( function (m) 0 == n %% m, 1:(n/2) )
Line 5,108:
=={{header|Racket}}==
With [[Proper_divisors#Racket]] in place:
<syntaxhighlight lang="racket">#lang racket
(require "proper-divisors.rkt")
(define SCOPE 20000)
Line 5,179:
(formerly Perl 6)
{{Works with|Rakudo|2019.03.1}}
<syntaxhighlight lang=perl6"raku" line>sub propdivsum (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
Line 5,202:
 
=={{header|REBOL}}==
<syntaxhighlight lang=REBOL"rebol">;- based on Lua code ;-)
 
sum-of-divisors: func[n /local sum][
Line 5,232:
 
=={{header|ReScript}}==
<syntaxhighlight lang="rescript">let isqrt = (v) => {
Belt.Float.toInt(
sqrt(Belt.Int.toFloat(v)))
Line 5,274:
=={{header|REXX}}==
===version 1, with factoring===
<syntaxhighlight lang="rexx">
/*REXX*/
 
Line 5,339:
 
CPU time consumption note: &nbsp; for every doubling of &nbsp; '''H''' &nbsp; (the upper limit for searches), &nbsp; the CPU time consumed triples.
<syntaxhighlight lang="rexx">/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H= 20000 /*get optional arguments (high limit).*/
w= length(H) ; low= 220 /*W: used for columnar output alignment*/
Line 5,386:
 
The optimization makes it about <u>another</u> &nbsp; '''30%''' &nbsp; faster when searching for amicable numbers up to one million.
<syntaxhighlight lang="rexx">/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w=length(H) ; low=220 /*W: used for columnar output alignment*/
Line 5,423:
 
The optimization makes it about <u>another</u> &nbsp; '''20%''' &nbsp; faster when searching for amicable numbers up to one million.
<syntaxhighlight lang="rexx">/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w= length(H) ; low= 220 /*W: used for columnar output alignment*/
Line 5,466:
 
The optimization makes it about <u>another</u> &nbsp; '''15%''' &nbsp; faster when searching for amicable numbers up to one million.
<syntaxhighlight lang="rexx">/*REXX program calculates and displays all amicable pairs up to a given number. */
parse arg H .; if H=='' | H=="," then H=20000 /*get optional arguments (high limit).*/
w= length(H) ; low= 220 /*W: used for columnar output alignment*/
Line 5,503:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
size = 18500
for n = 1 to size
Line 5,524:
=={{header|Ruby}}==
With [[proper_divisors#Ruby]] in place:
<syntaxhighlight lang="ruby">h = {}
(1..20_000).each{|n| h[n] = n.proper_divisors.sum }
h.select{|k,v| h[v] == k && k < v}.each do |key,val| # k<v filters out doubles and perfects
Line 5,542:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang=Runbasic"runbasic">size = 18500
for n = 1 to size
m = amicable(n)
Line 5,567:
=={{header|Rust}}==
 
<syntaxhighlight lang="rust">fn sum_of_divisors(val: u32) -> u32 {
(1..val/2+1).filter(|n| val % n == 0)
.fold(0, |sum, n| sum + n)
Line 5,595:
 
=={{header|Scala}}==
<syntaxhighlight lang=Scala"scala">def properDivisors(n: Int) = (1 to n/2).filter(i => n % i == 0)
val divisorsSum = (1 to 20000).map(i => i -> properDivisors(i).sum).toMap
val result = divisorsSum.filter(v => v._1 < v._2 && divisorsSum.get(v._2) == Some(v._1))
Line 5,605:
=={{header|Scheme}}==
 
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme inexact)
Line 5,670:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func propdivsum(n) {
n.sigma - n
}
Line 5,691:
 
=={{header|Swift}}==
<syntaxhighlight lang=Swift"swift">import func Darwin.sqrt
 
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
Line 5,746:
}</syntaxhighlight>
===Alternative===
about 800 times faster.<syntaxhighlight lang=Swift"swift">import func Darwin.sqrt
 
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
Line 5,802:
 
=={{header|tbas}}==
<syntaxhighlight lang="vb">
dim sums(20000)
 
Line 5,842:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc properDivisors {n} {
if {$n == 1} return
set divs 1
Line 5,912:
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">
#lang transd
 
Line 5,944:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">Input "Limit: ";l
Print "Amicable pairs < ";l
 
Line 6,008:
 
=={{header|UTFool}}==
<syntaxhighlight lang=UTFool"utfool">
···
http://rosettacode.org/wiki/Amicable_pairs
Line 6,030:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Option Explicit
Public Sub AmicablePairs()
Line 6,082:
=={{header|VBScript}}==
Not at all optimal. :-(
<syntaxhighlight lang=VBScript"vbscript">start = Now
Set nlookup = CreateObject("Scripting.Dictionary")
Set uniquepair = CreateObject("Scripting.Dictionary")
Line 6,131:
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang=Go"go">fn pfac_sum(i int) int {
mut sum := 0
for p := 1;p <= i/2;p++{
Line 6,170:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Int, Nums
 
Line 6,197:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">func SumDiv(Num); \Return sum of proper divisors of Num
int Num, Div, Sum, Quot;
[Div:= 2;
Line 6,239:
=={{header|Yabasic}}==
{{trans|Lua}}
<syntaxhighlight lang=Yabasic"yabasic">sub sumDivs(n)
local sum, d
Line 6,264:
=={{header|zkl}}==
Slooooow
<syntaxhighlight lang="zkl">fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }
const N=20000;
sums:=[1..N].pump(T(-1),fcn(n){ properDivs(n).sum(0) });
Line 6,274:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const MAXIMUM: u32 = 20_000;
 
// Fill up a given array with arr[n] = sum(propDivs(n))
Line 6,319:
=={{header|ZX Spectrum Basic}}==
{{trans|AWK}}
<syntaxhighlight lang="zxbasic">10 LET limit=20000
20 PRINT "Amicable pairs < ";limit
30 FOR n=1 TO limit
10,343

edits