These puzzles (most of them old classics) from various sources can be
used with
pupils who finish classwork early. Most of the questions were chosen
with enthusiastic, bright early teenagers in mind.
Some of the puzzles are also appropriate for class work - an initial
worked example on the board will help a lot.
There are a few trick questions. Some questions can be quickly answered
if you chance upon the right approach, but the 'long' solution isn't
too arduous. Several of the questions are best answered by writing a
computer program. Some are shown here.
Some worked examples are in J.A.H. Hunter's "Mathematical
Brain Teasers".
The following C++ code might help with the other problems
#include <vector>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
// This C++ program uses brute force to solve problems like
// THE +
// TEN +
// MEN
// ----
// MEET
//
// To solve a problem
// * List the words in the thewords variable
// * Set the leadingzeroesallowed, digitsunique, findallsolutions variables to true or false,
// depending of what you want.
// * Change the first line of the check routine to match the calculation.
// The words are referred to as thewords[0], thewords[1], etc so
// if (eval(thewords[0],v) + eval(thewords[1],v) + eval(thewords[2],v) == eval(thewords[3],v)) {
// means "if word1 + word2 + word3 equals word4 ..."
// * Re-compile and run the code.
// The code's not designed to be fast or easy to read, It solves
// THE + TEN + MEN = MEET almost instantly. SEND + MORE = MONEY takes over a minute.
string thewords[]={"THE","TEN", "MEN", "MEET"};
bool leadingzeroesallowed=false;
bool digitsunique=true;
bool findallsolutions=false;
string all="";
int numberofwords;
void initialise() {
numberofwords=sizeof(thewords)/sizeof(*thewords);
for (int i=0;i<numberofwords;i++)
all=all+thewords[i];
sort(all.begin(), all.end());
string::iterator it = unique (all.begin(), all.end());
all.resize( it - all.begin() );
}
int eval(string s, vector<int>v) {
int i=s.length();
int factor=1;
int sum=0;
while(i--) {
for (int j=0;j<all.length();j++)
if(s[i]==all[j]){
sum=sum+factor*v[j];
break;
}
factor=factor*10;
}
return sum;
}
void check(vector<int>v) {
//The next line may need changing
if (eval(thewords[0],v) + eval(thewords[1],v) + eval(thewords[2],v) == eval(thewords[3],v)) {
if(digitsunique) {
vector<int>v2=v;
sort(v2.begin(), v2.end());
vector<int>::iterator it = unique (v2.begin(), v2.end());
v2.resize( it - v2.begin() );
if (v2.size() != v.size())
return;
}
if(not leadingzeroesallowed) {
for (int j=0;j<all.length();j++) {
for (int k=0;k<numberofwords;k++)
if(thewords[k][0]==all[j]){
if (v[j]==0)
return;
}
}
}
cout << "Answer!" << endl;
for (int i=0; i<v.size() ; i++) {
cout << all[i] << " = " << v[i] << endl;
}
if (not findallsolutions)
exit(0);
}
}
void recurse(vector<int>v) {
if(v.size() >10) {
cout << "recursion level exceeded" << endl;
exit(0);
}
if (v.size()== all.length())
check(v);
else
for (int i=0;i<10;i++){
vector<int>v2=v;
v2.push_back(i);
recurse(v2);
}
}
int main() {
vector<int> v;
initialise();
for (int i=0;i<10;i++){
v.clear();
v.push_back(i);
recurse(v);
}
}
This is a Python 3 solution
import itertools
# FIVE + FIVE + NINE + ELEVEN + THIRTY
thewords=["FIVE", "FIVE", "NINE", "ELEVEN", "THIRTY"]
leadingzeroesallowed=False
findallsolutions=False
alltheuniqueletters=""
# Make a list of letters used
def initialise():
global alltheuniqueletters
for i in thewords:
alltheuniqueletters=alltheuniqueletters+i;
alltheuniqueletters=list(set(alltheuniqueletters))
alltheuniqueletters.sort()
def myeval(s,vd):
i=len(s)
factor=1
mysum=0
while i>0:
i=i-1
mysum=mysum+factor*vd[s[i]]
factor=factor*10
return mysum
def check(vd):
if myeval(thewords[0],vd) + myeval(thewords[1],vd) + myeval(thewords[2],vd) + myeval(thewords[3],vd)==myeval(thewords[4],vd):
if not leadingzeroesallowed:
for k in range(0,len(thewords)):
if vd[thewords[k][0]]==0:
return
print("Answer!")
print(vd)
if not findallsolutions:
exit()
initialise()
options=itertools.combinations(range(0,10), len(alltheuniqueletters))
for i in options:
for j in itertools.permutations(i):
vd=dict(zip(alltheuniqueletters,j))
check(vd)