Camel
Peter
Peter Campbell Smith

Chola and squares

Weekly challenge 109 — 19 April 2021

Week 109: 19 Apr 2021

Task 1

Task — Chowla numbers

Write a script to generate first 20 Chowla Numbers, named after Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as C(n) = sum of divisors of n except 1 and n.

Examples


C(n) for n = 1 .. 20
0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 
0, 20, 0, 21

Analysis

Sarvadaman Chowla
Sarvadaman Chowla

Sarvadaman Chowla was born in 1907 in London, the son of a mathematics professor. He studied maths in pre-partition India and at Cambridge, and taught at several universities in India and the USA.

His last appointment was at Penn State University, from which he retired in 1976, and which I joined as a lowly assistant professor the following year. Chowla died in 1995 and is remembered for several advances in number theory.

The Chowla function which generates his sequence is documented in the OEIS.

The sequence is not hard to calculate: set the values all initially to zero and then add in all the multiples of numbers between 2 and half the Chowla index. That algorithm calculates the first 10000 Chowla numbers in around 10 seconds on my leisurely Raspberry Pi computer.

Try it 

Try running the script with any input:



example: 100 - max 1000 please

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2021-04-19
use utf8;     # Week 109 - task 1 - Chowla numbers
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

chowla_numbers(20);
chowla_numbers(100);

sub chowla_numbers {
    
    my ($limit, @chowla, $j, $d);
    
    # initialise
    $limit = $_[0];
    $chowla[$_] = 0 for 1 .. $limit;
    
    # calculate numbers
    for $j (2 .. $limit) {
        for $d (2 .. int($j / 2)) {
            $chowla[$j] += $d if $d != $j and $j / $d == int($j / $d);
        }
    }
    
    # report
    say qq[\nInput:  \$limit = $limit];
    say qq[Output: ] . join(', ', @chowla[1 .. $limit]);
}

9 lines of code

I completed this challenge after the closing date
and it has not been submitted to GitHub

Output


Input:  $limit = 20
Output: 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14,
   0, 20, 0, 21

Input:  $limit = 100
Output: 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14,
   0, 20, 0, 21, 10, 13, 0, 35, 5, 15, 12, 27, 0, 41, 0,
   30, 14, 19, 12, 54, 0, 21, 16, 49, 0, 53, 0, 39, 32,
   25, 0, 75, 7, 42, 20, 45, 0, 65, 16, 63, 22, 31, 0,
   107, 0, 33, 40, 62, 18, 77, 0, 57, 26, 73, 0, 122, 0,
   39, 48, 63, 18, 89, 0, 105, 39, 43, 0, 139, 22, 45, 32,
   91, 0, 143, 20, 75, 34, 49, 24, 155, 0, 72, 56, 116

 

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