The Odyssey

I watched this film recently. Undeniably it is an epic film, but is it faithful to it’s source material?

At almost 3 hours running time, it contains a lot of the material from the original poem. However, it seems to focus on the events at Ithaca (before and after Odysseus’s return) at the expense of the rest of the story.

Another thing that I noticed was te absence of divine influence on the events. Whilst appreciating that this may have been done to cater for the tastes of the modern cinema-going audience, it took away from the narrative.

Most of the major events of the journey have been shown in a fleeting manner, apart from the episode with the Cyclops. However, even that has been changed from the original, in particular the escape from his cave. The land of the giants barely rates a few minutes, but Circe turning the travellers into pigs is related in detail.

Continue reading
Posted in Uncategorized | Tagged , | Leave a comment

8 Queens Problem

Today, we are going to look at the classic problem of placing Queens on a chessboard, such that no Queen is attacking any other Queens. It’s immediately obvious that we cannot place more than 8 Queens and meet these conditions, but can we place 8 Queens on the board? The answer is yes, we can.

One approach would be to place the Queens randomly on the board, then check for the conditions of the problem. Using this approach the first Queen can be placed on one of 64 squares, the next on one of 63 square, and so on. This would result in 64 * 63 * 62 * 61 * 60 * 59 * 58 *57 permutations, which is a very large number, so we can abandon that idea!

Continue reading
Posted in Mathematics, Uncategorized | Tagged | Leave a comment

10 Digit Number Puzzle

In my previous post I shared some code to generate permutations. We can use this code to solve a problem I’ve posted about previously. Find the only 10 digit number which uses each of the digits 0 – 9 and has the following property:

The first digit should be divisible by 1.
The number formed by the first 2 digits should be divisible by 2.
The number formed by the first 3 digits should be divisible by 3.
And so on until the number formed by the first 10 digits should be divisible by 10.

The 5th digit in our number will obviously need to be 5 and the tenth digit will need to be 0.  This still leaves us with 8 digits to place in the number.  8 digits can be selected  in 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 ways. This is usually written as 8! (pronounced factorial 8). In general, the factorial of an integer is the result of multiplying it and all lower integers down to 1. 8! is 40,320 which is a lot of numbers to try.

Luckily, we can reduce this in a few ways.  For a number to be divisible by an even digit (2, 4, 6 or 8), it has to be an even number, ie end in a 0, 2, 4, 6 or 8. So, the digits in the 2nd, 4th, 6th and 8th position in our final number have to be even, which means the digits in the first, 3rd, 7th and 9th position have to be odd. 

So, we can generate a list of the possible permutations of the odd digits, and another list of the possible permutations of the even digits. Interleave these with the 5 and the 0 to create a 10 digit number and test that against the conditions.  There are 4! possible permutations of the 4 odd digits, and the same for the even digits. 4! is 24, so we have a possible 24 * 24 numbers to test. This is 576, which is a vast reduction on our original 3,628,800.

For example, one of the possible combinations is ‘7139’ with ‘4268’.  Combining these with the ‘5’ and the ‘0’ gives ‘7412563890’.  Obviously, this does not satisfy the conditions of the puzzle, so we would need to look at the next permutation.

I’ve created a small html page to demonstrate this.  Most of it is self-explanatory, line 22 is a button which runs the Javascript code.  Lines 24 and 25 are two placeholders, one for the result and the second for any messages. Line 26 links to the Javascript code.

 1<!DOCTYPE html> 
 2<html> 
 3  <head>
 4    <title>Ten Digit Puzzle</title>
 5  </head>
 6  <body>
 7    <h1>10 Digit Number</h1>
 8    <h5>
 9	<p>Find the only 10 digit number which uses each of the digits 0 - 9 and has the following property.</p>
10	<p>The number formed by the first n digits should be divisible by n, ie
11	<ul>
12	  <li>The number formed by the first digit should be divisible by 1, a given.</li>
13	  <li>The number formed by the first 2 digits should be divisible by 2.</li> 
14	  <li>The number formed by the first 3 digits should be divisible by 3.</li>
15	  <li>The number formed by the first 4 digits should be divisible by 4.</li>
16	  <li>And so on until the number formed by the first 10 digits should be divisible by 10.</li>
17	</ul>
18	</p>
19	<p>Click on the button for the solution.</p> 
20    </h5>
21    <div>
22	<input id="clickMe"  type="button" value="Solution" />
23    </div>
24    <div id="results"></div>
25    <div id="message"></div>
26    <script type="text/javascript" src="tendigits.js"></script>
27  </body>
28</html>

This is the Javascript code: Line 1 adds a listener for the button click. Lines 4 and 5 get the placeholders for the results and messages. The string sOdd is initialised with ‘1379’ and cycles through the 24 permutations at line 30. For each iteration of sOdd, the string sEven is initialised with ‘2468’ and cycles through the 24 permutations at line 28. Lines 13 and 14 create the 10 digit number; this is checked in the loop from line 23. If successful, the result is displayed else a running count is displayed in the messages.

 1document.getElementById("clickMe").addEventListener("click", getSolution, false);
 2	
 3function getSolution() {			
 4  message = document.getElementById("message");
 5  results = document.getElementById("results");
 6  tested = 0;
 7  sOdd = '1379';
 8  while (sOdd != "") {
 9    sEven = "2468";
10    while (sEven != "") {
11	tested++;
12	//create candidate number
13	s = sOdd.substring(0, 1) + sEven.substring(0, 1) + sOdd.substring(1, 2) +         sEven.substring(1, 2) + "5" + 
14          sEven.substring(2, 3) + sOdd.substring(2, 3) + sEven.substring(3, 4) + sOdd.substring(3, 4) + "0";
15    	//check number
16    	success = true;
17    	for (z = 2; z < 9; z++) {
18	  x = parseInt(s.substring(0, z));
19	  if ((x % z) != 0) {
20	    success = false;
21	    break;
22	  }
23	}
24	if (success) results.innerHTML = results.innerHTML + "<p> Success " + tested + " " + s + "</p>"; else {
25	  if (message.innerText == "") message.innerHTML = "Failed: ";
26	  message.innerHTML = message.innerHTML + tested.toString().padStart(3, "0") + " ";
27	}
28	sEven = getNextPerm(sEven);
29    }
30    sOdd = getNextPerm(sOdd);
31  }	
32}
33				
34function getNextPerm(s) {
35  //create array of elements
36  n = s.length;
37  const elements = [];
38  for (z = 0; z < n; z++) elements[z] = s.substring(z, z + 1);
39  //find the largest index k such that elements[k] < elements[k + 1]
40  for (k = n - 2; k >= 0; k--) {
41    if (elements[k] < elements[k + 1]) {
42	//find the largest index l greater than k such that elements[k] < elements[l]
43	for (l = n - 1; l > k; l--) {
44	  if (elements[k] < elements[l]) {
45	    //swap elements k and l
46	    sTemp = elements[k];
47	    elements[k] = elements[l];
48	    elements[l] = sTemp;
49	    //reverse remainder of string
50	    k++;
51	    n--;
52	    while (k < n) {
53	      sTemp = elements[k];
54	      elements[k] = elements[n];
55	      elements[n] = sTemp;
56	      k++;
57	      n--;
58	    } 
59	    //create string from array elements
60	    t = "";
61	    for (z = 0; z < elements.length; z++) t = t + elements[z];
62	    return t;
63	  }
64	}
65    }
66  }
67  //not found
68  return "";
69}
70

You can test the code here.

Posted in Mathematics, Uncategorized | Tagged | Leave a comment

Dragon Age – The Veilguard

I played the first game in the Dragon Age series (‘Origins’) many, many years ago. It was an absolutely enthralling game. The choices you made in the game effected the entire game-world. Please see here for my initial reactions to that game.

The second instalment was Dragon Age 2, this was very disappointing. The consensus among many players was that Bioware had cut corners and rushed the game into production to cash in on the success of Origins.

Then came the third instalment, Dragon Age Inquisition. This was a definite improvement on the second part, but still not as good as the original. However, I did enjoy completing it and was hoping that Bioware would continue to improve the franchise.

There was a VERY LONG wait for the next instalment, Dragon Age – The Veilguard. When that came out the reviews were ecstatic, and I was looking forward to playing that. I was unable to play it at the time because I was busy with other things, and I felt that I should only play it when I could do it justice.

I have recently played it to completion, and it was very, very disappointing. Although it has not sunk to the dire depths of the second part, it is still not as good as the original, or even the third part.

I did not understand how it received so many glowing reviews on release, and then I saw that many sites had changed their rating for the game, downgrading it from their original assessment. I think they were caught up in the hype of the release and did not evaluate it as completely as they should have done.

The Veilguard is still a game worth playing, but do not expect the story telling of the original. I have read that we probably won’t see another release in this franchise, which is a relief. I just wish that they had not bothered with The Veilguard.

I was reminded of the Eye of the Beholder series. Part 1 was very good, part 2 was even better. I had played both of these on an Amiga, and was disappointed when I discovered part 3 would not be available on that platform. I actually abandoned my Amiga and bought a PC so I could play part 3 on it. It was utter TRASH! EOTB 3 remains one of the few games that I have started and never finished.

Posted in Uncategorized | Tagged | Leave a comment

Generating Permutations in Javascript

I’ve recently been working on a number puzzle which involved generating permutations. It involves finding permutations of a ten digit number. I decided to code this in Javascript. I found the following on Wikipedia:

The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place. 

Find the highest index i such that s[i] < s[i+1]. If no such index exists, the permutation is the last permutation.
Find the highest index j > i such that s[j] > s[i]. Such a j must exist, since i+1 is such an index.
Swap s[i] with s[j].
Reverse all the order of all of the elements after index i
.

This is my implementation of the above algorithm, adapted to generate all permutations after a given permutation, and allowing for user input and giving feedback:

document.getElementById("clickMe").addEventListener("click", getPermutations, false);

function getPermutations() {
message = document.getElementById("message");
s = document.getElementById("startPerm").value;
x = 1;
message.innerHTML = "<p>" + x + " " + s + "";
t = getNextPerm(s);
while (t != "") {
s = t;
x++;
message.innerHTML = message.innerHTML + "<p>" + x + " " + s + "</p>";
t = getNextPerm(s);
}
}

function getNextPerm(s) {
//create array of elements
n = s.length;
const elements = [];
for (z = 0; z < n; z++) elements[z] = s.substring(z, z + 1);
//find the largest index k such that elements[k] < elements[k + 1]
for (k = n - 2; k >= 0; k--) {
if (elements[k] < elements[k + 1]) {
//find the largest index l greater than k such that elements[k] < elements[l]
for (l = n - 1; l > k; l--) {
if (elements[k] < elements[l]) {
//swap elements k and l
sTemp = elements[k];
elements[k] = elements[l];
elements[l] = sTemp;
//reverse remainder of string
k++;
n--;
while (k < n) {
sTemp = elements[k];
elements[k] = elements[n];
elements[n] = sTemp;
k++;
n--;
}
//create string from array elements
t = "";
for (z = 0; z < elements.length; z++) t = t + elements[z];
return t;
}
}
}
}
//not found
return "";
}

You can test the code here (this will open the page in a new tab). Enter a piece of text and then click on the button. It will list permutations of the text.

I will be using this code in my next post to solve the puzzle.

Posted in Mathematics, Uncategorized | Tagged | Leave a comment