DEV Community

loading...

Weekly Challenge 109

simongreennet profile image Simon Green ・2 min read

I actually submitted my solutions last week, but forgot to write a blog post. Better late than never, eh? )

Challenge 109, My Solutions

TASK #1 › Chowla Numbers

Task

Write a script to generate first 20 Chowla Numbers, named after, Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as:

C(n) = sum of divisors of n except 1 and n

My solution

After submitting my solution, I realised that I over-engineered my solution. It's still functional though. This task is pretty straight forward. Work through the 1 to 20, and for each number find all divisors (other than 1 and the number itself). We can short cut this by only working through the first half since the second half cannot be divisors.

Example

» ./ch-1.pl 
0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21

Enter fullscreen mode Exit fullscreen mode

TASK #2 › Four Squares Puzzle

Task

You are given four squares as below with numbers named a,b,c,d,e,f,g. ... Write a script to place the given unique numbers in the square box so that sum of numbers in each box is the same.

My solution

This is a task where I assume some people will apply some math theory to the solution. For my I took an approach of brutal forcibly finding a solution. 7! = 5040 which is a piece of cake for any CPU these days.

  • The first part of the task is to get all 5,040 permutations. I have a rule of not using any module that isn't part of core Perl, so didn't use Algorithm::Permute to generate the sequence.
  • I then define an array @boxes to contain the values in each square, specifically [ 0, 1 ], [ 1, 2, 3 ], [ 3, 4, 5 ], [ 5, 6 ] (where a = 0, b = 1, etc.)
  • Finally I work through each permutation to see if it is a valid solution, and end when one is found.

Examples

My example differs from the one given in the example due to the way I order the numbers.

» ./ch-2.pl 1 2 3 4 5 6 7
a = 3
b = 7
c = 2
d = 1
e = 5
f = 4
g = 6
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app