Peter
Peter Campbell Smith

Odd numbers and
odder references

Weekly challenge 171 — 27 June 2022

Week 171: 27 Jun 2022

Task 1

Task — Abundant number

Write a script to generate the first 20 abundant odd numbers (AONs). According to Wikipedia, an abundant number is 'a number n for which the sum of divisors σ(n) > 2n, or, equivalently, the sum of proper divisors (or aliquot sum) s(n) > n.'

Examples


Example 1:
945 is the first abundant odd number as:
Sum of divisors:
1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 
135 + 189 + 315 = 975 which is greater than 945

Analysis

Wikipedia tells us that n is an abundant number if the sum of its divisors (including itself) σ(n) > 2n. Some abundant numbers are even, but we only want the odd ones.

Finding the divisors of a number isn't too hard, but I save myself the trouble by invoking Math::Prime::Util qw(divisors), and simply by checking successive odd integers we get the required 20 AONs in milliseconds. The 20th AON is 8925, so we don't need BigInt, and in fact the 200th is 92925 and even the 2000th is a manageable 1002375.

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2022-06-27
# PWC 171 task 1

use v5.26;
use strict;
use warnings;
use utf8;
use Math::Prime::Util qw(divisors);
binmode(STDOUT, ':utf8');

my ($count, $i, @divisors, $sum);

# loop over odd numbers
$count = 0;
for ($i = 1; $count < 20; $i += 2) {
    
    # add the divisors
    @divisors = divisors($i);
    $sum = 0;
    $sum += $_ for @divisors;
    
    # we have an AON if the sum exceeds 2i
    if ($sum > 2 * $i) {
        $count ++;
        say qq[aon[$count] = $i because σ($i) = $sum which is more than 2 * $i = ] . (2 * $i);
    }
}

Output

aon[1] = 945 because σ(945) = 1920 which is more than 2 * 945 = 1890
aon[2] = 1575 because σ(1575) = 3224 which is more than 2 * 1575 = 3150
aon[3] = 2205 because σ(2205) = 4446 which is more than 2 * 2205 = 4410
aon[4] = 2835 because σ(2835) = 5808 which is more than 2 * 2835 = 5670
aon[5] = 3465 because σ(3465) = 7488 which is more than 2 * 3465 = 6930
aon[6] = 4095 because σ(4095) = 8736 which is more than 2 * 4095 = 8190
aon[7] = 4725 because σ(4725) = 9920 which is more than 2 * 4725 = 9450
aon[8] = 5355 because σ(5355) = 11232 which is more than 2 * 5355 = 10710
aon[9] = 5775 because σ(5775) = 11904 which is more than 2 * 5775 = 11550
aon[10] = 5985 because σ(5985) = 12480 which is more than 2 * 5985 = 11970
aon[11] = 6435 because σ(6435) = 13104 which is more than 2 * 6435 = 12870
aon[12] = 6615 because σ(6615) = 13680 which is more than 2 * 6615 = 13230
aon[13] = 6825 because σ(6825) = 13888 which is more than 2 * 6825 = 13650
aon[14] = 7245 because σ(7245) = 14976 which is more than 2 * 7245 = 14490
aon[15] = 7425 because σ(7425) = 14880 which is more than 2 * 7425 = 14850
aon[16] = 7875 because σ(7875) = 16224 which is more than 2 * 7875 = 15750
aon[17] = 8085 because σ(8085) = 16416 which is more than 2 * 8085 = 16170
aon[18] = 8415 because σ(8415) = 16848 which is more than 2 * 8415 = 16830
aon[19] = 8505 because σ(8505) = 17472 which is more than 2 * 8505 = 17010
aon[20] = 8925 because σ(8925) = 17856 which is more than 2 * 8925 = 17850

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain