Peter
Peter Campbell Smith

Numbers in words
and Cardano triplets

Weekly challenge 148 — 17 January 2022

Week 148: 17 Jan 2022

Task 1

Task — Eban numbers

Write a script to generate all Eban Numbers <= 100. An Eban number is a number that has no letter ‘e’ in it when the number is spelled in English (American or British).

Examples


Example 1:
2, 4, 6, 30, 32 are the first 5 Eban numbers.

Analysis

Fortunately, I wrote a numbers-to-words function in Visual Basic about 20 years ago for the purpose of printing the words on cheques (US checks). Even more fortunately, I was able to find it, and it took little time to translate it into Perl.

Thereafter the solution is trivial.

It's mildly interesting that there are 19 Eban numbers under 100, but the twentieth one is 2000.

Script


#!/usr/bin/perl

# Peter Campbell Smith - 2022-01-17
# PWC 148 task 1

use v5.28;
use strict;
use warnings;
use utf8;

my ($j, $words, @digits, @tenties, @teens);

@digits = ((''), qw[one two three four five six seven eight nine]);
@tenties = (('', ''), qw[twenty thirty forty fifty sixty seventy eighty ninety]);
@teens = qw[ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen];

# the easy bit
say qq[\nAll Eban Numbers <= 100];
for $j (1 .. 100) {
    $words = words($j);
    say qq[$j - $words] unless $words =~ m|e|;
}

# convert an integer to words (works up to 999,999)
sub words {
        
    my ($number, $thousands, $words);
    $number = $_[0];
    
    # deal with thousands
    $thousands = int($number / 1000);
    $words = $thousands != 0 ? words3($thousands) . ' thousand' : '';

    # and the rest
    $number -= $thousands * 1000;
    $words = $words . ' and' if ($thousands != 0 and $number < 100 and $number != 0); 
    $words = $words . words3($number);
    $words =~ s|.||; # remove initial blank
    return $words;
}

sub words3 {

    # convert 1-999 into words
    my ($number, $hundreds, $words3, $tens, $units, $hyphen);
    return 'zero' unless $number = $_[0];
    $words3 = '';
        
    # hundreds
    $hundreds = int($number / 100);
    $words3 = $words3 . ' ' . $digits[$hundreds] . ' hundred' if $hundreds != 0;
    $number -= $hundreds * 100;
    if ($number) {
        $words3 = $words3 . ' and' if $hundreds != 0;
        
        # tens and units
        $tens = int($number / 10);
        $units = $number - 10 * $tens;
        if ($tens != 1) {
            $hyphen = ($units != 0 and $tens != 0) ? '-' : '';
            $words3 = $words3 . ' ' . $tenties[$tens] . $hyphen . $digits[$units];
        } else {
            $words3 = $words3 . ' ' . $teens[$units];
        }
    }

    return $words3;
}

Output


All Eban Numbers <= 100
2 - two
4 - four
6 - six
30 - thirty
32 - thirty-two
34 - thirty-four
36 - thirty-six
40 - forty
42 - forty-two
44 - forty-four
46 - forty-six
50 - fifty
52 - fifty-two
54 - fifty-four
56 - fifty-six
60 - sixty
62 - sixty-two
64 - sixty-four
66 - sixty-six

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain