Archive

Author Archive

A simple bubble sort

May 15th, 2013 No comments

A simple bubble sort which arranges the provided array of numbers from smallest to largest.

static void BubbleSort(ref int[] arr)
{
bool change = true;
int temp;

while (change)
{
change = false;
for (int i = 0; i < arr.GetLength(0) - 1; i++)
{
if (arr[i + 1] < arr[i])
{
change = true;
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
break;
}
}
}
}
Categories: C# Tags:

The Unique World of Anders Bjorkman aka Heiwa

January 4th, 2013 No comments

Anders Bjorkman (Heiwa) is completely nuts , check out the crazy things he believes.

https://sites.google.com/site/wtc7lies/home/anders-bjorkman-s-world

Categories: Wackos Tags:

Trolls: fantasy versus reality!

January 2nd, 2013 No comments

Categories: Main Tags:

Getting setup

November 26th, 2012 No comments

The RPi requires an SD Card in order to install an operating system (which I haven’t got), a mouse and keyboard (which I have) and something to connect its HDMI display to a monitor. As a Mac user this represents a problem, as I don’t have any displays with HDMI connectors on. However, I do have a 23″ Apple Cinema Display and a converter which converts the proprietary Apple connector to DVI output, so all I need is a DVI to VGA connector, a VGA to HDMI connector and a female to female VGA converter.

Categories: Raspberry Pi Tags:

Raspberry Pi – In the beginning

November 26th, 2012 No comments

I have acquired a Raspberry Pi Model B with the intention of doing some development work for a friend, and this blog shall document my findings, works, inevitable frustrations and, hopefully, my successes.

Categories: Raspberry Pi Tags:

.Net Balloon window

October 17th, 2012 No comments

Have you ever been frustated by the ToolTip component’s lack of functionality,  i.e. you’t can add a close button like the NotifyIcon control, lack of handleable events? Be frustated no more, as a solution is at hand.

The Balloon Window for .Net will is a handy addon that offers all the functionality of the NotifyIcon control AND the ToolTip in a handy package.

Categories: C#, Code Tags:

C: Returning an array from a function

December 14th, 2011 No comments

Recently I decided to learn C and have been diligently working my way through tutorials and websites, and since I’m more familiar with high level languages (C# for example) getting used to the more low level C is proving to be a challenge.

After getting to grips with the dreaded pointers I immediately ran into the problem of how to return an array from a function – the familiar methods used by anyone familiar with C# or VB.Net don’t work, and attempts at implementing return unusual results. Basically, populating an array in a function and passing a reference to it is doomed to fail it’s a regular local variable, with automatic duration, which means that it springs into existence when the function is called and disappears when the function returns. Passing a reference out of a function to a variable that has expired points to an junk.

After some googling, I found a helpful document that describes how to deal with the problem on http://c-faq.com, which describes in the three methods of returning arrays from functions which, briefly, are:

  1. The use of a static array within the function,
  2. Passing an array into the function and operating on it, and
  3. Creating an array in the function usign malloc.

All three methods have their own positives and negatives, but for the purposes on what I’m doing I’m going for the second one.

Categories: Code Tags:

Excel Macro: colour alternate ranges of the same value in the specified column

May 24th, 2011 No comments
'
'   Process a column of values, colouring alternate ranges of the same value.
'
'   Work down a column of cells, stopping when a blank is hit.
'
'   If a cell is a different value to the one preceding it, invert the
'   value of the toggle variable.
'
'   If the toggle variable is true colour the cell.
'
Public Sub main()

    Dim w As Worksheet
    Set w = Worksheets("Sheet1")
    
    Dim rowCtr As Integer
    rowCtr = 2
    
    Dim toggle As Boolean
    toggle = False
    
    While w.Cells(rowCtr, 1).Value <> ""
    
        If rowCtr > 2 Then
        
            If w.Cells(rowCtr, 2).Value <> w.Cells(rowCtr - 1, 2).Value Then
            
                toggle = Not toggle
            
            End If
        
        End If
        
        
        If toggle Then
        
            With w.Cells(rowCtr, 2).Interior
                .ColorIndex = 6
                .Pattern = xlSolid
            End With
        
        End If
        
    
        rowCtr = rowCtr + 1
    
    Wend

End Sub
Categories: Main Tags:

ASieve of Atkin prime number generator using TSQL

March 31st, 2011 No comments

I worked out an implementation of the Sieve of Atkin for use in solving Project Euler problems. It’s probably not as efficient as it could be, but it does the trick for me.

set nocount on

drop table #atkin
create table #atkin
(
	num float
	, isPrime bit
)
create clustered index primenums
	on #atkin(num)

declare @limit as bigint
declare @sqrt as float
declare @x as int
declare @y as int
declare @n as int

set @limit = 1000000
set @sqrt = sqrt(@limit)
set @x = 1

print 'begin outer loop'

/*
	put in candidate primes:
	integers which have an odd number of representations by certain quadratic forms
*/

while (@x <= @sqrt)
	begin

		set @y = 1

		while (@y <=@sqrt)
			begin

				set @n = (4 * (@x * @x)) + (@y * @y)
				if ((select num from #atkin where num =@n) is null)
					insert into #atkin (num, isPrime) values (@n,0)

				if (@n <= @limit AND (@n % 12 = 1 or @n % 12 =5))
					update #atkin set isPrime = isPrime ^ 1 where num = @n

				set @n = (3 * (@x * @x)) + (@y * @y)
				if ((select num from #atkin where num =@n) is null)
					insert into #atkin (num, isPrime) values (@n,0)

				if (@n <= @limit AND @n % 12 = 7)
					update #atkin set isPrime = isPrime ^ 1 where num = @n

				set @n = (3 * (@x * @x)) - (@y * @y)
				if ((select num from #atkin where num =@n) is null)
					insert into #atkin (num, isPrime) values (@n,0)

				if (@x >= @y AND @n <= @limit  AND @n % 12 = 11)
					update #atkin set isPrime = isPrime ^ 1 where num = @n

				set @y = @y + 1

			end

		set @x = @x + 1

	end 

declare @nsqr as bigint
declare @k as bigint

/*
	eliminate composites by sieving
*/
set @n = 5
while ( @n <= @sqrt)

	begin

		print @n

		if ((select isprime from #atkin where num = @n)=1)
			begin

				set @nsqr = @n * @n

				set @k = @nsqr

				while (@k <= @limit)

					begin

						update #atkin set isPrime = 0 where num = @k

						set @k = @k + @nsqr
						print '@k increment ' + str(@k)

					end

			end

		set @n = @n + 1

		print '@n increment ' + str(@n)

	end

	insert into #atkin (num, isPrime) values (2,1)
	insert into #atkin (num, isPrime) values (3,1)

delete from	#atkin where isprime <> 1
Categories: Project Euler, TSQL Tags:

Project Euler Problem 37 Solution in TSQL

March 28th, 2011 No comments

This is a solution for Project Euler Problem 37. It makes uses of a temporary table called #atkin which is populated with prime numbers using the TSQL routine that uses the Sieve of Atkin, which is also located on this site.

Easy! There are, however, a couple of deliberate errors, that can be easily located with some patience. I don’t want to make things to easy for you little scamps, do I?

/*
process the prime number list
*/

/*
the number we are examining can't be single digits or contain zeros
*/
drop table #candidates
select num
into #candidates
from #atkin
where charindex('0', cast(num as varchar)) = 0

declare @ctr as integer
declare @del as integer

set @ctr = 1
set @del = 0

/*
delete from the candidate list any number that doesn't have a
shorter counterpart in the list of prime numbers, increasing
the string size by 1 with every loop, and bailing out of when
we stop deleting things
*/
while (@del > 0)

begin

delete from #candidates
from #candidates c1
left outer join #atkin a1
on left(c1.num,@ctr) = a1.num
left outer join #atkin a2
on right(c1.num,@ctr) = a2.num
where a1.num is null
or a2.num is not null

set @del = @@rowCount
set @ctr = @ctr + 1

end

/*
output the answer
*/
select sum(num)
from #candidates
Categories: Project Euler Tags: