A computer science problem by Rocco Dalto

Computer Science Level pending

Let * represent the operation of multiplication of integers.

D O G T W O W H A K O \begin{array} { cccccc } & & & D & O & G \\ * & & & T & W & O \\ \hline & W & H & A & K & O \\ \end{array}

In the product shown above each letter represents a digit. Let a 1 a_{1} be the maximum value for W H A C K O \overline{WHACKO} , b 1 b_{1} be the minimum value for W H A C K O \overline{WHACKO} and n 1 n_{1} be all possible products of D O G T W O = W H A C K O \: \overline{DOG} * \overline{TWO} = \overline{WHACKO} and m 1 = a 1 b 1 + n 1 m_{1} = a_{1} - b_{1} + n_{1} .

D O G C A T A T R I P \begin{array} { cccccc } & & & D & O & G \\ * & & & C & A & T \\ \hline & A & T & R & I & P \\ \end{array}

In the second product shown above each letter represents a digit. Let a 2 a_{2} be the maximum value for A T R I P \overline{ATRIP} , b 2 b_{2} be the minimum value for A T R I P \overline{ATRIP} and n 2 n_{2} be all possible products of D O G C A T = A T R I P \: \overline{DOG} * \overline{CAT} = \overline{ATRIP} and m 2 = a 2 b 2 + n 2 m_{2} = a_{2} - b_{2} + n_{2} .

I A M A L I A R \begin{array} { cccccc } & & & & I \\ * & & & A & M \\ * & & & & A \\ \hline & L & I & A & R \\ \end{array}

In the third product shown above each letter represents a digit. Let a 3 a_{3} be the maximum value for L I A R \overline{LIAR} , b 3 b_{3} be the minimum value for L I A R \overline{LIAR} and n 3 n_{3} be all possible products of I A M A = L I A R \: \overline{I} * \overline{AM} * \overline{A} = \overline{LIAR} and m 3 = a 3 b 3 + n 3 m_{3} = a_{3} - b_{3} + n_{3} .

Find: m 1 + m 2 + m 3 m_{1} + m_{2} + m_{3} .

Note: You can create a program(in any language) to find m 1 m_{1} , m 2 m_{2} and m 3 m_{3} , but the program should not contain any predefined functions or procedures. That is you must create all functions and procedures and they should appear in the program, and not called from a library you created. For example, you could create your own maximum and minimum function for an array of integers and your own power function.

I wrote a program A A that when executed gets the input and writes a program B B to solve cryptograms to a text file, then I saved the text file using a different extension, complied it and ran program B B .

So, for the first cryptogram you would just need to run program A A and save the text file(using a different extension) containing program B 1 B_{1} and execute it, then run program A A again and save the text file(using a different extension) containing program B 2 B_{2} and execute it, then run program A A again and save the text file(using a different extension) containing program B 3 B_{3} and execute it.

I chose three cryptograms so that program A A can generate all three cryptograms as stated above. Writing three separate programs for each cryptogram would be tedious and that was not my intention. Essential, you just need to write program A A .

Assume I chose N N cryptograms. Write program A A to generate all N N cryptograms as stated above.

Program A A is just a slight adjustment of the Program in the previous problem.

Refer to previous problem. . .


The answer is 339522.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Rocco Dalto
Feb 26, 2017

I used Free Pascal to construct the program:

Program A : A:

program for_brillant; {To change to product}

uses mathunit;

type arraytype = array[1 .. 1000] of longint;

 chararray = array[1 .. 1000] of char;

var myfile:text;

s:string;

n:longint;

numwords:longint;

hold:text;

sizeword:arraytype;

procedure getinput;

var j:longint;

begin

writeln('Enter string of characters including * and =, but no spaces');

read(s);

n:= length(s);

writeln;

writeln('Enter number of words');

readln(numwords);

writeln;

writeln('Enter length of each word');

for j:= 1 to numwords do

begin

read(sizeword[j]);

end;

end;

procedure eliminate;

type darraytype = array[1 .. 300,1 .. 300] of char;

 strarray = array[1 .. 300] of string;

var j,k:longint;

notequal:boolean;

counter,counter2,total,total2,newtotal:longint;

w,z,y,x,p:chararray;

equal:boolean;

v:darraytype;

sum,prod:strarray;

e,a,b,c,f:string;

begin

counter:= 0;

for j:= 1 to n - 1 do

begin

notequal:= true;

for k:= j + 1 to n do begin

if (s[j] = s[k]) then notequal:= false;

end;

if notequal and (s[j] <> '*') and (s[j] <> '=') then

begin

counter:= counter + 1;

w[counter]:= s[j];

end;

end;

total:= counter + 1;

w[total]:= s[n];

z[1]:= s[1];

counter:= 1;

for j:= 1 to n do

begin

if (s[j] = '*') or (s[j] = '=') then

begin

counter:= counter + 1;

z[counter]:= s[j + 1];

end; end;

counter2:= 0;

for j:= 1 to counter - 1 do

begin

notequal:= true;

for k:= j + 1 to counter do

begin

if (z[j] = z[k]) then notequal:= false;

end;

if notequal then

begin

counter2:= counter2 + 1;

y[counter2]:= z[j];

end; end;

total2:= counter2 + 1;

y[total2]:= z[counter];

counter:= 0;

counter2:= 0;

for j:= 1 to total do

begin

notequal:= true;

for k:= 1 to total2 do

begin

if w[j] = y[k] then

begin

notequal:= false;

counter:= counter + 1;

x[counter]:= w[j]; end;

end;

if notequal then

begin

counter2:= counter2 + 1;

z[counter2]:= w[j];

end;

end;

assign(myfile, 'myfile2.txt');

rewrite(myfile);

writeln(myfile,'program for brillant 3;');

writeln(myfile,'type arraytype = array[1 .. 5000] of longint;');

writeln(myfile,'var z1:arraytype;');

writeln(myfile); writeln(myfile);

writeln(myfile,'function LINTpower(base,exponent:longint):longint;');

writeln(myfile,'var n,product:longint;');

writeln(myfile);

writeln(myfile,'begin');

    writeln(myfile,'product:= 1;');

writeln(myfile,'for n:= 1 to exponent do');

writeln(myfile,'begin');

writeln(myfile,'product:= base * product;');

writeln(myfile,'end;');

    writeln(myfile,'LINTpower:= product;');

writeln(myfile,'end;');

writeln(myfile);

writeln(myfile);

writeln(myfile, 'function minnum(a:arraytype; last:longint):longint;');

writeln(myfile,'var j,n:longint;');

writeln(myfile,'small:longint;');

writeln(myfile);

  writeln(myfile,'begin');

        writeln(myfile,'small:= a[1];');

writeln(myfile,'for j:= 2 to last do');

writeln(myfile,'begin');

writeln(myfile,'if a[j] < small then');

        writeln(myfile,'small:= a[j];');

writeln(myfile,'end;');

writeln(myfile,'minnum:= small;');

    writeln(myfile,'end;');

    writeln(myfile);

writeln(myfile,'function maxnum(a:arraytype; last:longint):longint;');

writeln(myfile,'var j,n:longint;');

writeln(myfile,'large:longint;');

  writeln(myfile,'begin');

        writeln(myfile,'large:= a[1];');

writeln(myfile,'for j:= 2 to last do');

writeln(myfile,'begin');

writeln(myfile,'if a[j] > large then');

        writeln(myfile,'large:= a[j];');

writeln(myfile,'end;');

writeln(myfile,'maxnum:= large;');

    writeln(myfile,'end;');

    writeln(myfile);

writeln(myfile,'procedure getprod; ');

writeln(myfile);

write(myfile,'var ');

for j:= 1 to counter do

begin

write(myfile,x[j],',');

end;

for j:= 1 to counter2 do

begin

if j < counter2 then

write(myfile, z[j],',')

else

write(myfile, z[j]);

end;

write(myfile,':longint;');

writeln(myfile);

write(myfile,' bool:boolean;');

writeln(myfile);

write(myfile,' j1:longint;');

writeln(myfile);

write(myfile,' a1,a2,a3,answer:longint; ');

writeln(myfile, 'prod:arraytype;');

writeln(myfile);

writeln(myfile);

writeln(myfile,'begin');

writeln(myfile);

writeln(myfile,'bool:= false;');

writeln(myfile,'j1:= 1;');

for j:= 1 to counter do

begin

writeln(myfile,'for ', x[j],':= 1 to 9 do ');

writeln(myfile,'begin')

end;

for j:= 1 to counter2 do

begin

writeln(myfile,'for ', z[j],':= 0 to 9 do ' );

writeln(myfile,'begin');

end;

for j:= 1 to counter do

begin

p[j]:= x[j];

end;

for j:= 1 to counter2 do

begin

p[j + counter]:= z[j];

end;

newtotal:= counter + counter2;

write(myfile,' if ');

for j:= 1 to newtotal - 1 do

begin

for k:= j + 1 to newtotal do

begin

if k mod 5 = 0 then

write(myfile);

writeln(myfile);

if j < newtotal - 1 then

write(myfile, '(',p[j], ' <> ', p[k],') and ')

else

write(myfile, '(',p[j], ' <> ', p[k],')');

end;

end;

write(myfile, ' then');

writeln(myfile);

writeln(myfile, 'begin' );

assign(hold,'hold.txt');

rewrite(hold);

for j:= 1 to n do

begin

if (s[j] <> '*') and (s[j] <> '=') then

begin

write(hold,s[j]);

end;

end;

reset(hold);

for j:= 1 to numwords do

begin

for k:= 1 to sizeword[j] do

begin

read(hold,v[k,j]);

end;

end;

sum[1]:= '';

for j:= 1 to numwords do

begin

if j <= numwords - 1 then

write(myfile,'z1[',j,']:= ')

else

write(myfile,'prod[j1]:= ');

for k:= 1 to sizeword[j] do

begin

str(sizeword[j] - k,e);

if k < sizeword[j] then

sum[j]:= sum[j] + ' ' + v[k,j] + ' * lintpower(10,' + e + ') + '

else

sum[j]:= sum[j] + ' ' + v[k,j] + ' * lintpower(10,' + e + ')';

end;

write(myfile,sum[j],';');

writeln(myfile);

end;

{put condition and equation here}

write(myfile,' if ');

a:= '';

for k:= 1 to numwords do

begin

if k <= numwords - 2 then

begin

str(k,b);

a:= a + 'z1[' + b + '] * '

end

else

begin

str(numwords - 1,c);

b:= 'z1[' + c + ']';

end;

f:= 'prod[j1]';

end;

write(myfile, a + b, ' = ', f);

write(myfile,' then ');

writeln(myfile,'begin');

writeln(myfile,'bool:= true;');

writeln(myfile,'j1:= j1 + 1;');

writeln(myfile,'end;');

writeln(myfile,'end;');

close(hold);

writeln(myfile);

for j:= 1 to newtotal do

begin

writeln(myfile, 'end;');

end;

writeln(myfile);

writeln(myfile,'if bool then');

writeln(myfile,'begin');

writeln(myfile,'a3:= j1 - 1;');

writeln(myfile);

writeln(myfile,'a2:= maxnum(prod,a3);');

writeln(myfile,'a1:= minnum(prod,a3);');

writeln(myfile,'writeln(''max = '', a2);');

writeln(myfile,'writeln(''min = '', a1);');

writeln(myfile,'writeln(''n = '', a3);');

writeln(myfile,'answer:= a2 - a1 + a3;');

writeln(myfile,'writeln(''answer = '', answer);');

writeln(myfile,'end;');

writeln(myfile);

writeln(myfile,' end; ');

writeln(myfile);

writeln(myfile);

writeln(myfile,'begin');

writeln(myfile,'getprod;');

writeln(myfile,'readln;');

writeln(myfile,'end.');

close(myfile);

end;

begin

getinput;

eliminate;

readln;

end.

Now run program A A and enter the data for the first cryptogram. Program A A has now written program B 1 B_{1} to the textfile "myfile2.txt".

For program B 1 : B_{1}:

Go to text file "myfile2.txt" and save it with a different extension( I saved it using using cryptogramprod1.pas).

Program cryptogramprod1.pas is:

program for brillant 3;

type arraytype = array[1 .. 5000] of longint;

var z1:arraytype;

function LINTpower(base,exponent:longint):longint;

var n,product:longint;

begin

product:= 1;

for n:= 1 to exponent do

begin

product:= base * product;

end;

LINTpower:= product;

end;

function minnum(a:arraytype; last:longint):longint;

var j,n:longint;

small:longint;

begin

small:= a[1];

for j:= 2 to last do

begin

if a[j] < small then

small:= a[j];

end;

minnum:= small;

end;

function maxnum(a:arraytype; last:longint):longint;

var j,n:longint;

large:longint;

begin

large:= a[1];

for j:= 2 to last do

begin

if a[j] > large then

large:= a[j];

end;

maxnum:= large;

end;

procedure getprod;

var d,t,w,g,h,a,c,k,o:longint;

bool:boolean;

j1:longint;

a1,a2,a3,answer:longint; prod:arraytype;

myfile:text; {added}

begin

assign(myfile,'justtest.txt'); {added}

rewrite(myfile);

bool:= false;

j1:= 1;

for d:= 1 to 9 do

begin

for t:= 1 to 9 do

begin

for w:= 1 to 9 do

begin

for g:= 0 to 9 do

begin

for h:= 0 to 9 do

begin

for a:= 0 to 9 do

begin

for c:= 0 to 9 do

begin

for k:= 0 to 9 do

begin

for o:= 0 to 9 do

begin

if

(d <> t) and

(d <> w) and

(d <> g) and

(d <> h) and

(d <> a) and

(d <> c) and

(d <> k) and

(d <> o) and

(t <> w) and

(t <> g) and

(t <> h) and

(t <> a) and

(t <> c) and

(t <> k) and

(t <> o) and

(w <> g) and

(w <> h) and

(w <> a) and

(w <> c) and

(w <> k) and

(w <> o) and

(g <> h) and

(g <> a) and

(g <> c) and

(g <> k) and

(g <> o) and

(h <> a) and

(h <> c) and

(h <> k) and

(h <> o) and

(a <> c) and

(a <> k) and

(a <> o) and

(c <> k) and

(c <> o) and

(k <> o) then

begin

z1[1]:= d * lintpower(10,2) + o * lintpower(10,1) + g * lintpower(10,0);

z1[2]:= t * lintpower(10,2) + w * lintpower(10,1) + o * lintpower(10,0);

prod[j1]:= w * lintpower(10,5) + h * lintpower(10,4) + a * lintpower(10,3) + c * lintpower(10,2) + k * lintpower(10,1) + o * lintpower(10,0);

if z1[1] * z1[2] = prod[j1] then begin

bool:= true;

writeln(myfile, z1[1], ' * ', z1[2],' = ', prod[j1]); {added}

j1:= j1 + 1;

end;

end;

end;

end;

end;

end;

end;

end;

end;

end;

end;

if bool then

begin

a3:= j1 - 1;

a2:= maxnum(prod,a3);

a1:= minnum(prod,a3);

writeln('max = ', a2);

writeln('min = ', a1);

writeln('n = ', a3);

answer:= a2 - a1 + a3;

writeln('answer = ', answer);

end;

close(myfile);

end;

begin

getprod;

readln;

end.

Running cryptogramprod1.pas we obtain:

a 1 = 487620 , b 1 = 213780 , n 1 = 3 m 1 = 273843 . a_{1} = 487620, \: b_{1} = 213780, \: n_{1} = 3 \implies m_{1} = \boxed{273843}.

I added an option to cryptogramprod1.pas for writing the products to a file, where I inserted {added}.

The products are:

509 * 420 = 213780

708 * 640 = 453120

903 * 540 = 487620

Now run program A A and enter the data for the second cryptogram. Program A A has now written program B 2 B_{2} to the textfile "myfile2.txt".

For program B 2 : B_{2}:

Go to text file "myfile2.txt" and save it with a different extension( I saved it using using cryptogramprod2.pas).

Program cryptogramprod2.pas is:

program for brillant 3;

type arraytype = array[1 .. 5000] of longint;

var z1:arraytype;

function LINTpower(base,exponent:longint):longint;

var n,product:longint;

begin

product:= 1;

for n:= 1 to exponent do

begin

product:= base * product;

end;

LINTpower:= product;

end;

function minnum(a:arraytype; last:longint):longint;

var j,n:longint;

small:longint;

begin

small:= a[1];

for j:= 2 to last do

begin

if a[j] < small then

small:= a[j];

end;

minnum:= small;

end;

function maxnum(a:arraytype; last:longint):longint;

var j,n:longint;

large:longint;

begin

large:= a[1];

for j:= 2 to last do

begin

if a[j] > large then

large:= a[j];

end;

maxnum:= large;

end;

procedure getprod;

var d,c,a,o,g,t,r,i,p:longint;

bool:boolean;

j1:longint;

a1,a2,a3,answer:longint; prod:arraytype;

myfile:text; {added}

begin

assign(myfile,'justtest2.txt'); {added}

rewrite(myfile);

bool:= false;

j1:= 1;

for d:= 1 to 9 do

begin

for c:= 1 to 9 do

begin

for a:= 1 to 9 do

begin

for o:= 0 to 9 do

begin

for g:= 0 to 9 do

begin

for t:= 0 to 9 do

begin

for r:= 0 to 9 do

begin

for i:= 0 to 9 do

begin

for p:= 0 to 9 do

begin

if

(d <> c) and

(d <> a) and

(d <> o) and

(d <> g) and

(d <> t) and

(d <> r) and

(d <> i) and

(d <> p) and

(c <> a) and

(c <> o) and

(c <> g) and

(c <> t) and

(c <> r) and

(c <> i) and

(c <> p) and

(a <> o) and

(a <> g) and

(a <> t) and

(a <> r) and

(a <> i) and

(a <> p) and

(o <> g) and

(o <> t) and

(o <> r) and

(o <> i) and

(o <> p) and

(g <> t) and

(g <> r) and

(g <> i) and

(g <> p) and

(t <> r) and

(t <> i) and

(t <> p) and

(r <> i) and

(r <> p) and

(i <> p) then

begin

z1[1]:= d * lintpower(10,2) + o * lintpower(10,1) + g * lintpower(10,0);

z1[2]:= c * lintpower(10,2) + a * lintpower(10,1) + t * lintpower(10,0); prod[j1]:= a * lintpower(10,4) + t * lintpower(10,3) + r * lintpower(10,2) + i * lintpower(10,1) + p * lintpower(10,0);

if z1[1] * z1[2] = prod[j1] then begin

bool:= true;

writeln(myfile, z1[1], ' * ', z1[2],' = ', prod[j1]); {added}

j1:= j1 + 1;

end;

end;

end;

end;

end;

end;

end;

end;

end;

end;

end;

if bool then

begin

a3:= j1 - 1;

a2:= maxnum(prod,a3);

a1:= minnum(prod,a3);

writeln('max = ', a2);

writeln('min = ', a1);

writeln('n = ', a3);

answer:= a2 - a1 + a3;

writeln('answer = ', answer);

end;

close(myfile); {added}

end;

begin

getprod;

readln;

end.

Running cryptogramprod2.pas we obtain:

a 2 = 98072 , b 2 = 34706 , n 2 = 7 m 2 = 63373 . a_{2} = 98072, \: b_{2} = 34706, \: n_{2} = 7 \implies m_{2} = \boxed{63373}.

I added an option to cryptogramprod2.pas for writing the products to a file, where I inserted {added}.

The products are:

195 * 248 = 48360

148 * 352 = 52096

198 * 374 = 74052

164 * 598 = 98072

259 * 134 = 34706

286 * 139 = 39754

395 * 164 = 64780

Now run program A A and enter the data for the third cryptogram. Program A A has now written program B 3 B_{3} to the textfile "myfile2.txt".

For program B 3 : B_{3}:

Go to text file "myfile2.txt" and save it with a different extension( I saved it using using cryptogramprod3.pas).

Program cryptogramprod3.pas is:

program for brillant 3;

type arraytype = array[1 .. 5000] of longint;

var z1:arraytype;

function LINTpower(base,exponent:longint):longint;

var n,product:longint;

begin

product:= 1;

for n:= 1 to exponent do

begin

product:= base * product;

end;

LINTpower:= product;

end;

function minnum(a:arraytype; last:longint):longint;

var j,n:longint;

small:longint;

begin

small:= a[1];

for j:= 2 to last do

begin

if a[j] < small then

small:= a[j];

end;

minnum:= small;

end;

function maxnum(a:arraytype; last:longint):longint;

var j,n:longint;

large:longint;

begin

large:= a[1];

for j:= 2 to last do

begin

if a[j] > large then

large:= a[j];

end;

maxnum:= large;

end;

procedure getsum;

var l,i,a,m,r:longint;

bool:boolean;

j1:longint;

a1,a2,a3,answer:longint; prod:arraytype;

myfile:text; {added}

begin

assign(myfile,'justtest3.txt'); {added}

rewrite(myfile);

bool:= false;

j1:= 1;

for l:= 1 to 9 do

begin

for i:= 1 to 9 do

begin

for a:= 1 to 9 do

begin

for m:= 0 to 9 do

begin

for r:= 0 to 9 do

begin

if

(l <> i) and

(l <> a) and

(l <> m) and

(l <> r) and

(i <> a) and

(i <> m) and

(i <> r) and

(a <> m) and

(a <> r) and

(m <> r) then

begin

z1[1]:= i * lintpower(10,0);

z1[2]:= a * lintpower(10,1) + m * lintpower(10,0);

z1[3]:= a * lintpower(10,0); prod[j1]:= l * lintpower(10,3) + i * lintpower(10,2) + a * lintpower(10,1) + r * lintpower(10,0);

if z1[1] * z1[2] * z1[3] = prod[j1] then begin

bool:= true;

writeln(myfile, z1[1], ' * ', z1[2],' * ', z1[3],' = ', prod[j1]);{added}

j1:= j1 + 1;

end;

end;

end;

end;

end;

end;

end;

if bool then

begin

a3:= j1 - 1;

a2:= maxnum(prod,a3);

a1:= minnum(prod,a3);

writeln('max = ', a2);

writeln('min = ', a1);

writeln('n = ', a3);

answer:= a2 - a1 + a3;

writeln('answer = ', answer);

end;

close(myfile); {added}

end;

begin

getsum;

readln;

end.

Running cryptogramprod3.pas we obtain:

a 3 = 5796 , b 3 = 3492 , n 3 = 2 m 3 = 2306 . a_{3} = 5796, \: b_{3} = 3492, \: n_{3} = 2 \implies m_{3} = \boxed{2306}.

I added an option to cryptogramprod3.pas for writing the products to a file, where I inserted {added}.

The products are:

4 * 97 * 9 = 3492

7 * 92 * 9 = 5796

m 1 + m 2 + m 3 = 339522 . \therefore m_{1} + m_{2} + m_{3} = \boxed{339522}.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...