Sunday, January 20, 2013
Saturday, January 5, 2013
A grid where rows, columns and diagonals are equal
My son showed me trick where I chose a number between 20 and 99 (inclusive) and he would then create a 4-by-4 grid of numbers where all the numbers would equal that number.
I thought I would write the algorithm. I thought it might be interesting for him to see how how a computer program worked. I thought C# would be easy so I used Mono on the mac.
[Main.cs]
[NumberGrid]
using System;
namespace AllWaysAddUp
{
public class NumberGrid
{
private int[,] _grid;
{
_grid = new int [4,4] {{-1, 1, 12, 7} , {11, 8, -1, 2} ,{5, 10, 3,-1} ,{4,-1, 6, 9}} ;
}
public int GetCell(int rowNumber, int columnNumber)
{
return _grid[rowNumber, columnNumber];
}
public void calculate(int number)
{
_grid[0,0] = number -20;
_grid[1,2] = _grid[0,0] - 1;
_grid[3,1] = _grid[0,0] + 1;
_grid[2,3] = _grid[3,1] + 1;
}
}
}
I thought I would write the algorithm. I thought it might be interesting for him to see how how a computer program worked. I thought C# would be easy so I used Mono on the mac.
[Main.cs]
using System;
namespace AllWaysAddUp
{
class MainClass
{
private static NumberGrid _numberGrid;
public static void Main (string[] args)
{
Console.WriteLine ("Enter a number between 20 and 99 and this will calculate"+ '\n' +
"a grid of 4 rows and 4 columns where all the rows, columns"+ '\n' +
"and diagonals all add up to that number"+ '\n' + '\n'+
"Enter a number between 20 and 99...");
string s = Console.ReadLine();
int i;
if(!int.TryParse(s, out i))
Console.WriteLine("You did not enter a number");
else {
if( i < 20 || i > 99)
Console.WriteLine("Number not in range. Number must be between 20 and 99.");
else {
_numberGrid = new NumberGrid();
_numberGrid.calculate(i);
PrintNumberGrid();
}
}
Console.ReadLine();
}
private static void PrintNumberGrid()
{
for (int row = 0; row < 4; row++) {
Console.WriteLine();
for (int col = 0; col < 4; col++) {
int num = _numberGrid.GetCell(row, col);
Console.Write (" {0}" + '\t', num.ToString());
}
}
}
}
}
{
public class NumberGrid
{
private int[,] _grid;
// Array format: {row0 col0, row0 col1, row0 col2, row0 col3 }
// {row1 col0, row1 col1, row1 col2, row1 col3 }
// {row2 col0, row2 col1, row2 col2, row2 col3 }
// {row3 col0, row3 col1, row3 col2, row3 col3 }
public NumberGrid (){
_grid = new int [4,4] {{-1, 1, 12, 7} , {11, 8, -1, 2} ,{5, 10, 3,-1} ,{4,-1, 6, 9}} ;
}
{
return _grid[rowNumber, columnNumber];
}
{
_grid[0,0] = number -20;
_grid[1,2] = _grid[0,0] - 1;
_grid[3,1] = _grid[0,0] + 1;
_grid[2,3] = _grid[3,1] + 1;
}
}
}
How to test memory in OSX
If your are experiencing an occasional random rebooting then maybe there is a memory gremlin.
A pretty good old-school memory test can be done by downloading memtest and installing it, then starting your Mac in command line mode (single user mode), and then running memtest.
Multiple tests?
:/ root# memtest all 5
(Running 5 times took over 5 hours)
Example Output
Memtest version 4.22 (64-bit)
Copyright (C) 2004 Charles Cazabon
Copyright (C) 204-2008 Tony Scaminaci (Macintosh port)
Licensed under the GNU General Public Licence version 2 only
Max OS X 10.8.2 (12C60) running in single user mode
Memory Page Size: 4096
System has 2 Intel core(s) with SSE
Requested memory: 3864MB (4052697088 bytes)
Available memory: 3864MB (4052697088 bytes)
NOTE: Memory request is too large, reducing to acceptable value...
Allocating memory: 3756MB (3939221568 bytes) at local address 0x0000000101000000
Attempting memory lock...locked successfully
Partitioning memory into 2 comparison buffers...
Buffer A: 1878MB (196910784 bytes) starts at local address 0x0000000101000000
Buffer B: 1878MB (196910784 bytes) starts at local address 0x000000017665e020
Running 5 test sequences... (CTRL-C to quit)
Test sequence 1 of 5:
Running tests on full 3756MB region...
Stuck Address : ok
Linear PRN : ok
Running comparison tests using 1878MB buffers...
Random Value : ok
Compare XOR : ok
Compare SYB : ok
Compare MUL : ok
Compare DIV : ok
Compare OR : ok
Compare AND : ok
Sequential Increment: ok
Solid bits : ok
Block Sequential : ok
Checkerboard : ok
Bit Spread : ok
Bit Flip : ok
Walking Ones : ok
Walking Zeroes : ok
A pretty good old-school memory test can be done by downloading memtest and installing it, then starting your Mac in command line mode (single user mode), and then running memtest.
- Download memtest
- Install. It will be placed in /usr/bin/
- Shut down the mac and restart keeping the cmd and s keys depressed. It will boot into single user mode.
- Run memtest by typing, memtest all
Where to get memtest
It can be found in a few places and I have used it from here before, http://www.optilabs.eu/download/memtest_422.zip
Multiple tests?
memtest all n
:/ root# memtest all 5
(Running 5 times took over 5 hours)
Example Output
Memtest version 4.22 (64-bit)
Copyright (C) 2004 Charles Cazabon
Copyright (C) 204-2008 Tony Scaminaci (Macintosh port)
Licensed under the GNU General Public Licence version 2 only
Max OS X 10.8.2 (12C60) running in single user mode
Memory Page Size: 4096
System has 2 Intel core(s) with SSE
Requested memory: 3864MB (4052697088 bytes)
Available memory: 3864MB (4052697088 bytes)
NOTE: Memory request is too large, reducing to acceptable value...
Allocating memory: 3756MB (3939221568 bytes) at local address 0x0000000101000000
Attempting memory lock...locked successfully
Partitioning memory into 2 comparison buffers...
Buffer A: 1878MB (196910784 bytes) starts at local address 0x0000000101000000
Buffer B: 1878MB (196910784 bytes) starts at local address 0x000000017665e020
Running 5 test sequences... (CTRL-C to quit)
Test sequence 1 of 5:
Running tests on full 3756MB region...
Stuck Address : ok
Linear PRN : ok
Running comparison tests using 1878MB buffers...
Random Value : ok
Compare XOR : ok
Compare SYB : ok
Compare MUL : ok
Compare DIV : ok
Compare OR : ok
Compare AND : ok
Sequential Increment: ok
Solid bits : ok
Block Sequential : ok
Checkerboard : ok
Bit Spread : ok
Bit Flip : ok
Walking Ones : ok
Walking Zeroes : ok
Subscribe to:
Posts (Atom)