Posts

Abundantly odd and ... Oh dear!

 Abundant odd numbers Task 1 asks us to write a script to generate first 20 Abundant Odd Numbers (AONs). 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. References to functions Task 2 asks: Create sub compose($f, $g) which takes in two parameters $f and $g as subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) = $f->($g->($x)) Hmm. I have written something like a million lines of code over 50 years in IT, and the number of times I have felt the need for a reference to a function is quite small - in

Primorials and Kronecker products

Primorials I am always amazed at the way mathematicians find a meaning or a use for obscure sequences and mathematical operations. And here's another one: primorial P(n) is defined as the product of the first n prime numbers. That is true whether or not you accept 1 as the first prime, but apparently P(0) is 1 which neatly sidesteps that argument. It's easy to see that successive primorials increase fast. We are asked to generate the first 10, and P(9) is already over 223 million, and after that we are quickly into BigInt territory. As this is a relatively simple task I chose to generate the primes rather than using a module and my algorithm looks like this: k = 1 Loop j from 2 until we've found P(9)      Next j if j is divisible by any prime we've found so far     So j is prime, save it     k = k * j, and k is the next primorial I'm sure someone will come with a one-liner for this, but as always I tens to prioritise lucidity over brevity. Kronecker products You com

Brilliant and Achilles

Brilliant numbers A brilliant number has exactly two prime factors, and they both have the same number of digits. That sounds like an easily met pair of criteria.  For example, there are 21 2-digit prime numbers, yielding 231 brilliant numbers ranging from 441 to 9409, so they're not rare. The ever-helpful Math::Prime::Util has a function factor  which returns a list of prime factors of its argument. so the algorithm looks like this: loop over test from 1 to something big until we've found 20 brilliant numbers     get the prime factors (pf) of test     skip to the next test unless pf contains exactly 2 same-length members      test is brilliant! end That generates the desired 20 numbers in microseconds, 2000 in under a second and 20 000 in under 15 seconds on my lowly Raspberry Pi.  The 20 000th brilliant number is  2 733 299, which is 1117 x 2447. Achilles numbers Write a script to generate first 20 Achilles Numbers: numbers that are powerful but imperfect. A positive integer 

More funny numbers ... and a very big one

Perrin primes The Perrin sequence is defined to start with 3, 0, 2 and after that, term n is the sum of terms n - 2 and n - 3. So far as I can see this sequence has no obvious practical use, but that's what pure mathematicians like.  Perrin primes are then those terms in the sequence which are prime numbers. Looking at the supplied value of f(13) it seems wise to invoke our old friend Math::BigInt, and the only other quirk is that some early numbers - such as 5 - appear more than once so duplicates need to be avoided. f(23) is (allegedly): 363164016399244815805032197910163452342446707053298994058937620089599954252132412186574487308402607836559211310382905704431937109345826731415063277024792603788022650498093625791060248194801884136245414356244053719051489817377617669359842639508618961672266009887958633066461382309019736040977943759168952083749283051316305477706149140125981757254619775310985719999323688197165625540103979982057963031539821586634974261143232900735399735249444398601731

Prime, ePrim, mePri, imePr, rimeP ... and Gamma

Circular primes Task 1 asks us to identify the first 10 circular primes: that is, prime numbers which when permuted by repeatedly moving the last digit to the start are still prime. Although not stated, but implied by the supplied answer, the answers are all the lowest value permutation of the given string of digits - so for example 197 is an answer but not 719 or 971. My simple solution uses good old Eratosthenes's sieve to generate the primes. Then for each prime we can permute the digits, immediately moving on the next candidate if: the permuted value is not prime, or the permuted value matches an already-identified circular prime (for the reason stated in the second paragraph above) Since Mohammad was kind enough to share the answer, we know that the 10th circular prime is a 6-digit number - 199 933 - and we might be tempted to limit the size of our sieve to that number.  However, some of its permutations considerably exceed the number itself, for example 999 331, so I ran the
D00DlE5 We are asked for a list of 2-8 letter words comprising which can be spelled with the symbols ABCDEF0157 with 0 read as O, 1 as either I or L, 5 as S and 7 as T. The operations required on any word are: discard if not 2-8 letters long discard if contain anything not in ABCDEFOLIST anything left is a solution From the supplied dictionary there are 1464 such words. I found that having 1 represent I or L made for some hard to read solutions such as 1111C17 (illicit), so eliminating 1 -> L reduced the number to 693. For the first optional extra I limited the number of 'special' substitutions to 3, which reduced the number to 559, losing such words as DE5I57ED (desisted). The second optional extra, requiring us to make 8-letter phrases from these words is a little trickier. Given that the words vary from 2-8 letters long, they could be combined as follows: 2 + 6, 3 + 5, 4 + 4, 2 + 2 + 4, 2 + 3 + 3, 2 + 2 + 2 + 2 Of the 559 words from optional extra 1, 15 have 2 letters, 69

Dots, lines and whatever fits best

Image
Scalable Vector Graphics SVG has always been a popular technology with me. Amongst other things, I've used it for making scale drawings for alterations to my house, and for making easily scalable or re-colourable logos for websites. For images which don't contain photographic material SVG files are often tiny in size when compared to compressed bitmaps like JPEG or PNG. But although many (most?) web browsers can render them, it's quite unusual to find them in use. In my experience, Inkscape is the best GUI editor for SVG graphics, though its output is rather more verbose than that from the Perl SVG module. Both tasks this week require us to draw lines and points in a 2-dimensional space. Once you've mastered the syntax required, the task is fairly simple, although there is one catch to be surmounted, which is that SVG's geometry is 'left-handed'. The normal convention for Cartesian (ie x and y) coordinates is that the positive direction of the y axis is 90