30 March 2022
All about primes
22 March 2022
Mean Brazilians
Task 1 - Pythagorean means
I'm never quite sure what is wanted or considered good in these tasks. The options seem to be:
- There's a module to do this
- I can do this in one somewhat inscrutable line
- I can do this in a readable way with lots of comments
Task 2 - Brazilian numbers
17 March 2022
Pernicious and weird ...
Pernicious numbers
... are positive integers which have a prime number of ones in their binary representation, and they can presumably have any number of zeroes as well.
Perl conveniently has the $binary = sprintf('%b', $n) construct which quickly gives us the binary representation of $n, and we can then count the 1s like this:
@ones = $binary =~ m|1|g;
This rather counterintuitive statement works because the m|| returns a list of matches which can then be assigned to an array, and the number of elements in the array thus gives the number of matches, or in our case, the number of 1s.
And as we've dealt with the generation of primes often in the last few weeks I took the lazy way out and used Math::Prime::Util 'is_prime' to determine whether the count is prime.
Weird numbers
... are defined as those where the sum of the proper divisors (divisors including 1 but not the number itself) is greater than the number, but no subset of those divisors sums to the number.
So this breaks down to three steps:
- Find the proper divisors of n
- Check that their sum is greater than n
- Check that no combination of the divisors sums to n
10 March 2022
Bigint week
Both our tasks this week require us to use integers greater than 2^64. I used the Math::Bigint module to handle those.
Task 1: Fortunate numbers
- Loop over n = 1 to something big
- Calculate the product of the first n primes (pn)
- Loop over m = 2 to something else big
- Calculate pn + m and check if it's prime
Task 2: Fibonacci related patterns
- Create the Fibonacci series up to a big number (big)
- Create a string by concatenating the members of the series, modulo n
- Loop over 2 to a medium-sized number - this is the length of the repeat, pp
- So the unit that (maybe) repeats comprises chars 0 to pp-1 of the string
- Make another string containing enough copies of the unit to be longer than big, and then truncate it to big characters
- We have a result if 'another string' is the same as 'string'.