The Weekly Challenge 003

5-Smooth Numbers and Pascal's Triangle!

Original Challenge Link | My Solutions

Task 1: 5-smooth numbers

"The Ugly Numbers: When Primes Stay Small!"

Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5. They are also called Hamming/Regular/Ugly numbers.

The Strategy: Check if a number's only prime factors are 2, 3, or 5 by dividing repeatedly. Generate numbers until we have the required count.
Perl Implementation
sub is_smooth {
    my $n = shift;
    while ( $n % 2 == 0 ) { $n /= 2; }
    while ( $n % 3 == 0 ) { $n /= 3; }
    while ( $n % 5 == 0 ) { $n /= 5; }
    return $n == 1;
}

Task 2: Pascal's Triangle

"The Triangle of Numbers: When Each Row Adds Up!"

Create a script that generates Pascal Triangle. Accept number of rows from the command line.

The Strategy: Build each row by adding the two numbers above it. Each row starts and ends with 1.