## MindCipher

### May 10, 2013

For the first problem we perform a simple simulation:

`(define (flips rev)`

(define (flip) (if (< (rand) 1/2) 'H 'T))

(let loop ((k 3) (fs (list (flip) (flip) (flip))))

(if (equal? fs rev) k

(loop (+ k 1) (take 3 (cons (flip) fs))))))

Here *rev* is a list of coin flips in reverse order, since the coin flips are added to the front of the list. One call to `flips`

computes the average number of flips to find the desired sequence. The two functions below compute the average over *n* trials:

`(define (monday n)`

(let loop ((i 0) (k 0))

(if (= i n) (/ k n 1.0)

(loop (+ i 1) (+ k (flips '(H T H)))))))

`(define (tuesday n)`

(let loop ((i 0) (k 0))

(if (= i n) (/ k n 1.0)

(loop (+ i 1) (+ k (flips '(T T H)))))))

Here are our results:

`> (monday 1000)`

9.996

> (tuesday 1000)

8.058

Thus Monday is likely to be higher than Tuesday. That makes sense because on Monday the new pattern starts fresh after a two-shot miss, while on Tuesday the new pattern has a head start of one flip after a two-shot miss. That is, if you flip heads-then-tails on Monday, then flip a tail to miss the pattern, you have to wait for the next heads-then-tails for the next possible hit. But on Tuesday, if you flip heads-then-tails, then flip a head to miss the pattern, you already have the first flip of the next heads-then-tails pattern.

The second problem is easily solved by splitting the number into pieces and checking each year after 1978 in order until the answer is found:

`> (let loop ((n 1979))`

(let ((front (quotient n 100))

(middle (modulo (quotient n 10) 100))

(back (modulo n 100)))

(if (= (+ front back) middle) n

(loop (+ n 1)))))

2307

If you don’t like the brute force solution, you can represent a year as 1000*a* + 100*b* + 10*c* + *d*, employ a little bit of algebra to form a diophantine equation, then solve.

The third problem is a trick: (*p* + *q*) / 2 is between *p* and *q*, and by definition there are no primes between *p* and *q*, so the answer is *no*, with no programming required.

We used `take`

and `rand`

from the Standard Prelude. You can run the programs at http://programmingpraxis.codepad.org/fpSuFV7A. Be sure to take a look at MindCipher; some of the problems there will turn your brain inside-out.

[…] today’s Programming Praxis exercise, our goal is to solve two exercises from the MindCipher website […]

My Haskell solution (see http://bonsaicode.wordpress.com/2013/05/10/programming-praxis-mindcipher/ for a version with comments):

Ans 2) 2307

n = 1979

while n<9999:

if (n/100) + (n%100) == (n/10)%100:

print n

break

n=n+1

1.’s most accurate answer is probably ‘there is no way to know’ (in fact, since one can only do something a finite number of times in a day, there’s a (tiny) nonzero chance you didn’t see the sequences at all on either day). However, we can get the average time until one sees any particular 3-element code using a Markov chain with 9 states.

Source

Mean time to hit TTT = 14

Mean time to hit TTH = 8

Mean time to hit THT = 10

Mean time to hit THH = 8

Mean time to hit HTT = 8

Mean time to hit HTH = 10

Mean time to hit HHT = 8

Mean time to hit HHH = 14

E(HTH) > E(HTT), so one would probably see Monday’s average as longer than Tuesday’s.

here’s my solution to 1978 in python:

@eupraxia

I’m not sure (not familiar with the language) but if you’re only counting triples, you’re missing the first two tosses. (It takes 3 to get a triple in the first place, which will be the first you test).

@namako. Many thanks. You’re absolutely right, I should have started the count at three, not one.

The coin tossing problem comparing the statistics on two different

targets “HTH” versus “HTT” can be resolved with some logical

analysis:

Both targets are a specific permutation of three flips of a

coin.

Coin flipping continues until the target permutation occurs.

After each coin flip that fails to create a matching

permutation, one or none previous flips can be used as part

of the permutation that includes the next flip.

For the HTH target:

A failing sequence of “HTT” requires at least three more

tosses; none of the previous tosses can be used for a

successfully matched permutation. e.g.: HTT{HTH}

A failing sequence of “HH” requires two more tosses, because

the second toss can be part of succesful matching sequence:

H{HTH}.

For the HTT target:

A failing sequence of “HTH” can be successfully matched with

two more tosses: “HT{HTT}”.

A failing sequence of “HH” can be successfully matched with

just two more tosses: “H{HTT}”.

So, the number of tosses needed for a succesful match, including

a random number of failing tosses is smaller for the “HTT”

target than for the “HTH” target.

I wrote a Ruby program to demonstrate the problem:

Here’s the run output:

For the primes problem (#3), here is a J program to show that there are no primes in the set of (p+q)/2 where p,q are consecutive primes in the first million prime numbers.

For number 3:

p and q are consecutive primes.

p and q are positive integers

let m = (p + q)/2

m is the mean of p

p < m < q

therefore, m cannot be prime, because p and q are consecutive primes

For number 2:

The end of my previous comment should have said: Based on observation 1, there are at most 8 possibilities for 1bcd, 7 for 2bcd, etc. for a total of (8*(8+1))/2 = 36 possible solutions between the years 1000 and 9999

// solution to 1978 prob. using Java

public class Year1978Problem {

/**

* @param args

*/

public static final int REF_YEAR = 1978;

public static final int DEFAULT_VAL = 0;

private int findNextYear(){

boolean yearFound = false;

int i = REF_YEAR; // (1978)

int nextYear;

do{

i++; // starting the test with 1979

int a = i/100; // to get (19)

int b = i%100; // to get (79)

int c = a%10; // to get (9)

int d = b/10; // to get (7)

int e = Integer.parseInt((Integer.toString(c)+d)); // to get (97)

if((a+b) == e){ // checking whether 1979 is OK

yearFound = true;

return i; // return if yes,

}

}while(!yearFound); // continue till finding the year

return DEFAULT_VAL;

}

public static void main(String[] args) {

Year1978Problem obj = new Year1978Problem();

int nextYear = obj.findNextYear();

System.out.println(“Next year satisfying given condition : “+nextYear);

}

}

# Next year satisfying given condition : 2307

bool check = false;

int _refYear = 1978;

while (check == false)

{

_refYear += 1;

check = CheckUp(_refYear);

}

Console.Write(“\n Next to 1978 is ” + _refYear + “.”);

Console.ReadLine();

Environment.Exit(0);

}

//METHODS

public static bool CheckUp(int entry)

{

string _year = string.Empty;

_year = Convert.ToString(entry);

int _num1 = Convert.ToInt32(_year[0].ToString() + _year[1].ToString());

int _num2 = Convert.ToInt32(_year[2].ToString() + _year[3].ToString());

int _sum = _num1 + _num2;

string _new = _year[1].ToString() + _year[2].ToString();

if (_new == Convert.ToString(_sum))

{

return true;

}

return false;

}

I have to learn Coldfusion for my job, so I’ve been using different examples from your site to help me learn. Most solutions I’ve seen to 1978 solve it by splitting it up into a,b,c,d. I chose a different route because I felt the individual digits didn’t mean as much as the two digits together. Is there anything wrong with me solving it this way?

Year Program

Find the next year after 1978 in which the middle digits is equals to the sum of the first two digits and the last two digits

#NumberFormat(a, mask)# + #NumberFormat(c, mask)# = #NumberFormat(b, mask)#

Sorry, last one I placed inside

`instead of`

For #2:

#!/usr/bin/ruby

r=[];1978.upto(9999) {|y|y=y.to_s;((“#{y[0]}#{y[1]}”.to_i+”#{y[2]}#{y[3]}”.

to_i)==(“#{y[1]}#{y[2]}”.to_i))?r.push(y):”; };puts r.to_s

That will give you all the solutions that are in a 4 digit year. You can easily quit after the first by just printing (instead of pushing to an array and exiting):

#!/usr/bin/ruby

1978.upto(9999) {|y|y=y.to_s;((“#{y[0]}#{y[1]}”.to_i+”#{y[2]}#{y[3]}”.

to_i)==(“#{y[1]}#{y[2]}”.to_i))?puts y;exit:”; }