100 DECLARE EXTERNAL FUNCTION sum_proper_divisors 110 CLEAR 120 ! 130 DIM f(20001) ! sum of proper factors for each n 140 FOR i=1 TO 20000 150 LET f(i)=sum_proper_divisors(i) 160 NEXT i 170 ! look for pairs 180 FOR i=1 TO 20000 190 FOR j=i+1 TO 20000 200 IF f(i)=j AND i=f(j) THEN 210 PRINT "Amicable pair ";i;" ";j 220 END IF 230 NEXT j 240 NEXT i 250 ! 260 PRINT 270 PRINT "-- found all amicable pairs" 280 END 290 ! 300 ! Compute the sum of proper divisors of given number 310 ! 320 EXTERNAL FUNCTION sum_proper_divisors(n) 330 ! 340 IF n>1 THEN ! n must be 2 or larger 350 LET sum=1 ! start with 1 360 LET root=SQR(n) ! note that root is an integer 370 ! check possible factors, up to sqrt 380 FOR i=2 TO root 390 IF MOD(n,i)=0 THEN 400 LET sum=sum+i ! i is a factor 410 IF i*i<>n THEN ! check i is not actual square root of n 420 LET sum=sum+n/i ! so n/i will also be a factor 430 END IF 440 END IF 450 NEXT i 460 END IF 470 LET sum_proper_divisors = sum 480 END FUNCTION