[Question]
Given the equation 28x +30y + 31z = 365 and that x, y, and z are natural
numbers. Can we predict the value of x + y + z? Discuss and show your
thinking.
[Anwser]
Natural number => integer number greater than 0
Determine the range for each variable
y=1,z=1 then x=10.85, 1 <= x <= 10
x=1,z=1 then y=10.20, 1 <= y <= 10
x=1,y=1 then z=9.90, 1 <= z <= 9
Rearrange equation
28x +30y + 31z = 365
28(x+y+z) + (2y+3z) = 365
x+y+z = [ 365 - (2y+3z) ] / 28
x+y+z has to be whole number
therefore 365-(2y+3z) has to be multiple of 28
Tabulate data
x+y+z | 365-(2y+3z) | (2y+3z) | 5<=(2y+3z)<=47 |
10 | 280 | 85 | Out of range |
11 | 308 | 57 | Out of range |
12 | 336 | 29 | OK |
13 | 364 | 1 | Out of range |
ANSWER: x+y+z is 12
So, what is x, y, z.
Let's try the brute force methods in
AWK, BC, Perl, Ruby, SH, Tcl
Answer is given at the end of this blog.
# AWK
awk '
END {
for(x=1;x<=10;++x) {
for(y=1;y<=10;++y) {
for(z=1;z<=9;++z) {
s=28*x+30*y+31*z
if (s==365) {
print x, "+", y, "+", z, "=", s;
}
}
}
}
}' /dev/null
# BC
for(x=1;x<=10;++x) {
for(y=1;y<=10;++y) {
for(z=1;z<=9;++z) {
s=28*x+30*y+31*z
if(s==365) {
x
"+"
y
"+"
z
"="
s
}
}
}
}
# Perl
foreach $x (1..10) {
foreach $y (1..10) {
foreach $z (1..9) {
$s=28*$x+30*$y+31*$z;
if ( $s == 365 ) {
print $x, "+", $y, "+", $z, "=", $s, "\n";
}
}
}
}
# Ruby
(1..10).each do
|x|
(1..10).each do
|y|
(1..9).each do
|z|
s=28*x+30*y+31*z
if s==365
print x, "+", y, "+", z, "=", s, "\n"
end
end
end
end
# SH, extremely slow
#
# Works in Linux, Cygwin
# if you do not have "seq" in your system, define this
# seq()
# {
# awk 'END{for(i='$1';i<='$2';++i) {print i}}' /dev/null
# }
for x in `seq 1 10`; do
for y in `seq 1 10`; do
for z in `seq 1 9`; do
s=`expr 28 \* $x + 30 \* $y + 31 \* $z`
if [ $s -eq 365 ]; then
echo "$x + $y + $z = $s"
fi
done
done
done
# Tcl: Method 1
for { set x 1 } { $x <= 10 } { incr x } {
for { set y 1 } { $y <= 10 } { incr y } {
for { set z 1 } { $z <= 9 } { incr z } {
set s [expr {28*$x + 30*$y + 31*$z}]
if { $s == 365 } {
puts "$x + $y + $z = $s"
}
}
}
}
# Tcl: Method 2
foreach x {1 2 3 4 5 6 7 8 9 10} {
foreach y {1 2 3 4 5 6 7 8 9 10} {
foreach z {1 2 3 4 5 6 7 8 9} {
set s [expr {28*$x + 30*$y + 31*$z}]
if { $s == 365 } {
puts "$x + $y + $z = $s"
}
}
}
}
# Tcl: Method 3
set nine {1 2 3 4 5 6 7 8 9}
set ten {1 2 3 4 5 6 7 8 9 10}
foreach x $ten {
foreach y $ten {
foreach z $nine {
set s [expr {28*$x + 30*$y + 31*$z}]
if { $s == 365 } {
puts "$x + $y + $z = $s"
}
}
}
}
[ANSWER]
x=1, y=4, z=7
x=2, y=1, z=9