Posts

Showing posts from June, 2022

PWC #171

PWC #171 Challenge 1 Odd Abundant Numbers Abundant numbers are whole numbers (positive integers) where the sum of all the whole-number divisors is greater than the number itself (where the summation includes both prime and non-prime divisors and the number 1, but not the number itself) . They contrast with perfect numbers, where this sum exactly equals the number, and deficient numbers, where this sum is less than the number. We are asked to find the first 20 odd abundant numbers. I use the Math::Factor::XS package in Perl 5. This has the "factors" function which returns an array of all the factors of a number including non-prime factors (but excluding 1 and the number itself). Perl 5 does not have a ready-made sum function to sum over an array, but it is trivial to write one, along with another convenience function to print an array. We then loop over the odd numbers using a C-style for loop (for my $n=1;;$n+=2), calculating the sum of factors (remembering to add 1), and

PWC #170

Perl weekly challenge #170 In Perl weekly challenge #170 , we are asked to complete two tasks in Perl 5 and/or Raku. Challenge 1 Compute a primorial number primorial(n) P_n(n) = P(1)*P(2)*...*P(n) where P(i) is the i'th prime, e.g., P(1)=2, P(2)=3, ... Like a factorial, except multiplying primes in sequence rather than positive integers. The challenge requires listing the first 10 primorials. I used the Math::Prime::Util::GMP library, which I had used earlier for Challenge #168, which included a compute-intensive task involving primes. For that challenge, this library gave speed and power rivaling optimized C code. It is a very impressive library. If I ever need to do serious work involving big prime numbers, Perl would be a top choice because of this. The library has two different built-in functions directly calculating primorials. I would have just used one of them if I had read the meta-CPAN docs carefully enough (even if just for a code challenge, why write an interpreted home-