-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.pl
More file actions
89 lines (70 loc) · 1.78 KB
/
17.pl
File metadata and controls
89 lines (70 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl
$count = 0;
for($i=1;$i<1000;$i++){
$temp = $i;
$word = 0;
if($temp > 99){
$word += 7; ## if the number is over 100, add 'hundred' (7)
$stemp = sprintf("%d", $temp);
$word += Single(substr($stemp,0,1));
$temp -= (int(substr($stemp,0,1)) * 100);
if($temp != 0){
$word += 3; ## Adding the 'and' if it is needed
}
}
print ("Temp = $temp\n");
if($temp > 19){
$stemp = sprintf("%d", $temp);
$word += Double(substr($stemp,0,1));
# print("Temp = $temp\n");
$temp -= (int(substr($stemp,0,1))*10);
# print("Temp = $temp\n");
}
print ("Temp = $temp\n");
if($temp > 9){
$stemp = sprintf("%d", $temp);
$word += Teen(substr($stemp,1,1));
$temp = 0;
}
print ("Temp = $temp\n");
if($temp > 0){
$stemp = sprintf("%d", $temp);
$word += Single(substr($stemp,0,1));
}
$count += $word;
print "$i = $word\n";
print "Count = $count\n\n";
}
$count += 11;
print("Result: $count\n");
#Be sure to add eleven to the final number for 'one thousand'
sub Single {
#Subroutine to process single numbers and starts of hundreds
my $val = 0;
my ($w) = @_;
$val = 3 if $w == 1 || $w == 2 || $w == 6;
$val = 5 if $w == 3 || $w == 7 || $w == 8;
$val = 4 if $w == 4 || $w == 5 || $w == 9;
return $val;
}
sub Double {
#Subroutine to process double (twenty, thirty, etc.) numbers
my $val = 0;
my ($w) = @_;
$val = 3 if $w == 1;
$val = 6 if $w == 2 || $w == 3 || $w == 8 || $w == 9;
$val = 5 if $w == 4 || $w == 5 || $w == 6;
$val = 7 if $w == 7;
return $val;
}
sub Teen{
#subroutine to handle teens
my $val = 0;
my ($w) = @_;
$val = 3 if $w == 0;
$val = 6 if $w == 1 || $w == 2;
$val = 8 if $w == 3 || $w == 4 || $w == 8 || $w == 9;
$val = 7 if $w == 5 || $w == 6;
$val = 9 if $w == 7;
return $val;
}