DEV Community

Simon Green
Simon Green

Posted on • Updated on

Look mum, no inputs ๐Ÿ˜›

Weekly Challenge 147

Challenge, My solutions

Unless I'm mistaken, this is the first week where both challenges have had no inputs. It is always tempted to use a single print statement, but that defeats the purpose of why we do them, isn't it?

TASK #1 โ€บ Truncatable Prime

Task

Write a script to generate first 20 left-truncatable prime numbers in base 10.

In number theory, a left-truncatable prime is a prime number which, in a given base, contains no 0, and if the leading left digit is successively removed, then all resulting numbers are primes.

My solution

Let's start off with the straight forward task. I use the is_prime method that I've used in previous tasks, slightly tweaked because zero is not a prime.

I then have a is_trunc_prime method that works out if every left truncated number of the number supplied is a prime, and it contains no zeros. To make things as quick as possible, I start with the most truncated number first. As Python treats integers and strings differently, I turn the input into a string to perform the truncation.

The main method then has a loop that exits when the solutions list when there are 20 items. Each iteration of the loop increments the number we check by one.

The Perl code is a transliteration of the Python code.

Examples

$ ./ch-1.py
2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197

$ ./ch-1.pl 
2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197
Enter fullscreen mode Exit fullscreen mode

TASK #2 โ€บ Pentagon Numbers

Task

Write a sript[sic] to find the first pair of Pentagon Numbers whose sum and difference are also a Pentagon Number.

Pentagon numbers can be defined as P(n) = n(3n - 1)/2.

My solution

Let's clear one thing up first. The first solution is likely to be P(0) and P(0) as 0 + 0 and 0 - 0 is zero, which is also P(0). However, I don't think that is the solution we are actually looking for. For the same reason, I'm not comparing identical pentagonal numbers in my solution.

Thanks to Wikipedia's page on Pentagonal Numbers, we learn out we can use the calculation below to find out if the number is Pentagonal, and what it's index is.

Image description

Now I don't even believe how you get the opposite of (3n2 - n) รท 2 to be the above, but it does work. I can tell you after I've done my pull request, I'm straight over to Abigail's blog to see the number theory behind this one :-)

For this task, I have two methods. The first is pentagon_number. This turns the index into a number, so 4 into 22 for example. The other method is is_pentagon_number and does the opposite using the calculation mentioned in the Wikipedia page, so 22 into 4 for example.

It then becomes a task of finding the first pair. For this I have a counter p1 that starts at two. I then have an inner loop p2 that goes from one to p1 - 1. I work out if the sum and subtraction of the two numbers are also pentagonal values, and if so display the results and exit the loops.

The Perl solution is also a transliteration of the Python one, with one caveat. In Perl x % y is always in integer even if x isn't. Therefore I needed to add an addition check to make sure x is an integer.

Examples

 ./ch-2.py 
P(2167) + P(1020) = 7042750 + 1560090 = 8602840 = P(2395)
P(2167) - P(1020) = 7042750 - 1560090 = 5482660 = P(1912)

$ ./ch-2.pl 
P(2167) + P(1020) = 7042750 + 1560090 = 8602840  = P(2395)
P(2167) - P(1020) = 7042750 - 1560090 = 5482660  = P(1912)
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)