Killing spiders - a fresh look on Chowla Numbers

TL;DR

Don’t burn the house to kill a spider.

In previous post PWC109 - Chowla Numbers I gave a very inefficient solution to the challenge:

sub gcd { my ($A, $B) = @_; ($A, $B) = ($B % $A, $A) while $A; return $B }

sub chowla_number ($n) { sum(grep { gcd($n, $_) == $_ } 2 .. $n - 1) // 0 }

Well, it was more inefficient than I was thinking at the time. And I am lucky that Colin Crain reviews all the submissions and provides fruitful comments on the solutions (Colin Crain β€Ί Perl Weekly Review #109).

Now… emojis are me, reading through the post and… taking action:

The most basic way to determine whether a number is a divisor of another number is to try the division and see it there is any remainder.

🧐 πŸ’ͺ πŸ‘

[…] Flavio has provided us with something completely different: using a greatest common divisor routine to vet the divisor candidates.

🀭 😱 🀯

I find this somewhat analogous to burning down the house to kill a spider, but it gets the job done and certainly made me think.

πŸ™„ πŸ˜‚

sub chowla_number ($n) { sum(grep { !($n % $_) } 2 .. $n - 1) // 0 }

πŸ€—

So there you have it: feedback is key! Thanks Colin πŸ˜„


Comments? Octodon, , GitHub, Reddit, or drop me a line!