The Weekly Challenge 008

Perfect Numbers and Centering Strings

Solutions on GitHub

Task 1: Perfect Numbers

Write a script that computes the first five perfect numbers.

A perfect number is an integer that is equal to the sum of its positive proper divisors (all divisors excluding the number itself).

Output

The first five perfect numbers are: 6, 28, 496, 8128, 33550336

Logic

Finding perfect numbers directly via division becomes extremely slow. Instead, we can utilize Euclid–Euler Theorem: every even perfect number can be represented as $2^{p-1} \times (2^p - 1)$ where both $p$ and $2^p - 1$ are prime numbers (a Mersenne prime). This allows us to compute perfect numbers near-instantaneously!

Perl Solution

ch-1.pl

use strict; use warnings; sub is_prime { my $num = shift; return 0 if ( $num < 2 ); for ( my $i = 2 ; $i <= sqrt($num) ; $i++ ) { return 0 if ( $num % $i == 0 ); } return 1; } sub get_next_prime { my $num = shift; $num++; while ( !is_prime($num) ) { $num++; } return $num; } my @perfect_numbers = (); my $p = 2; my $num = 2**( $p - 1 ) * ( 2**$p - 1 ); while ( scalar(@perfect_numbers) < 5 ) { push @perfect_numbers, $num; $p = get_next_prime($p); $num = 2**( $p - 1 ) * ( 2**$p - 1 ); } print "The first five perfect numbers are: "; print join( ", ", @perfect_numbers );

Python Solution

ch-1.py

def is_prime(num: int) -> bool: if num < 2: return False for i in range(2, int(num**0.5) + 1): if num % i == 0: return False return True def get_next_prime(num: int) -> int: num += 1 while not is_prime(num): num += 1 return num perfect_numbers = [] p = 2 num = 2 ** (p - 1) * (2**p - 1) while len(perfect_numbers) < 5: perfect_numbers.append(num) p = get_next_prime(p) num = 2 ** (p - 1) * (2**p - 1) print("The first five perfect numbers are:", perfect_numbers)

Task 2: Centering Strings

Write a function named center that takes a list of strings as its argument and centers them.

Example

Input: "This", "is", "a test of the", "center function" Output: This is a test of the center function

Logic

First, scan the list of strings to find the maximum length. Then, iterate through each string, computing the necessary left-padding as (max_length - length(string)) / 2. Add that number of space characters to the start of the string to center it.

Perl Solution

ch-2.pl

sub center { my @lines = @_; # Find the maximum length of the lines my $max_length = 0; foreach my $line (@lines) { my $length = length($line); if ( $length > $max_length ) { $max_length = $length; } } # Center each line by adding spaces at the beginning my @centered_lines; foreach my $line (@lines) { my $padding = ( $max_length - length($line) ) / 2; my $centered_line = ' ' x int($padding) . $line; push @centered_lines, $centered_line; } return @centered_lines; } print( center( "This", "is", "a test of the", "center function" ) );

Python Solution

ch-2.py

from typing import List def center(lines: List[str]) -> List[str]: # Find the maximum length of the lines max_length = max(len(line) for line in lines) # Center each line by adding spaces at the beginning centered_lines = [] for line in lines: padding = (max_length - len(line)) // 2 centered_line = " " * padding + line centered_lines.append(centered_line) return centered_lines