MindCipher
May 10, 2013
According to their masthead, the MindCipher website is “a social repository of the world’s greatest brain teasers, logic puzzles and mental challenges.” Some of the problems lend themselves to brute force computation, others are better solved with pencil-and-paper or solely with mental effort. We look at three MindCipher exercises today.
1: Coin Flip: On Monday, you flip a coin all day. You start flipping it until you see the pattern Head, Tail, Head. You record the number of flips required to reach this pattern, and start flipping again (and counting up from 1 again) until you see that pattern again, you record the second number, and start again. At the end of the day you average all of the numbers you’ve recorded. On Tuesday you do the EXACT same thing except you flip until you see the pattern Head, Tail, Tail.
Will Monday’s number be higher than Tuesday’s, equal to Tuesday’s, or lower than Tuesday’s?
2: 1978: The year 1978 is such that the sum of the first two digits and the latter two digits is equal to the middle two digits, i.e. 19 + 78 = 97. What is the next year (after 1978) for which this is true?
3: Sum of Two Prime Numbers: If p, q > 2 are consecutive in set of primes. Since p,q can only be odd number, (p+q) is an even number.
Can (p+q)/2 be prime?
Your task is to solve the three problems given above; write a computer program to help you if you wish. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coins.rb
hosted with ❤ by GitHub
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test-prime-pair-sums.ijs
hosted with ❤ by GitHub
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:”; }
Namako is basically right about problem 1. The two cases both amount to finding the expected hitting time for a Markov chain with FOUR states (counting the number of matched coins), from which a simple calculation — inv(I-Q)U where I is the identity matrix, Q is the transition matrix excluding the fourth state, and U is an all-ones column vector, then take the first element — gives the *exact result 10 for HTH and 8 for HTT.
What’s not obvious from that is what the standard deviations are.
A simulation can give that quite simply.
Monday => (count: 1000000 min: 3 max: 112 mean: 9.996582000000235 sd: 7.614711570447746 skew: 2.029387841503218)
Tuesday => (count: 1000000 min: 3 max: 71 mean: 7.994588000000178 sd: 4.892717307278136 skew: 1.833047061117329)
I tackled problem 2 by brute force.
(1000 to: 3000) select: [:year | (year // 100) + (year \ 100) = (year // 10 \ 100)]