RESTful (Representational State Transfer) web services offer an easy alternative to SOAP. Pete writes about the comparisons between REST and SOAP see PeteFreitag.
Missing number in a sequence problem
I was recently given a problem to find a missing number in a sequence of numbers from 1..100 in the quickest way possible. The problem suggested that I have some java code that receives the stream of integers from 1 to 100 in a random order, however with one number missing (we make some assumptions here that I know when the stream is finished).
1. The most obvious solution would be to sort the numbers ascending, then count from 1..100 until we find the missing one. But this can be very slow for larger ranges i.e. 10 million.
2. Another solution is to sort the numbers ascending and then add the numbers at opposite ends working your way to the middle i.e 1+100, 2+99, 3+98, 4+97 which in each case add upto 101 each time. This way we only loop through half the sequence, with one loop starting at the beginning and the other one from the end. Then, if we suddenly get a result=100, then we know we have just skipped a higher number, and likewise if the result=102, then we have just skipped a lower number.
3. The third solution I came across is to realise that the sequence of numbers is a simple arithmetic progression. We can work out the sum simply by N(N+1)/2 where N represents the number of elements. So we can get the expected sum without any form of looping, and also we have the actual sum of the numbers which we can easily work out by adding the numbers up as they are streamed in. Simple subtraction of the 2 sums gives us the missing number.
Posted in Lab49
Prime Numbers in Java
I recently came across an interesting piece of code in C# to generate prime numbers, I have converted it to Java here. This example prints out the primes upto 1000, but can easily be adapted.
private static void findPrimes()
{
int max = 1000;
boolean[] nonPrimes = new boolean[1000];
for (int i = 2; i<max; i++ ) {
if (!nonPrimes[i]) {
for (int j = i*i; j<max; j+=i) {
nonPrimes[j] = true;
}
System.out.println(i);
}
}
}
The interesting thing about this code is that it basically works it’s way through the time tables, i.e. starts off with the 2 times table eliminating elements that are mulitples of 2, then does the same with 3, then 4 etc… Each loop of j starts with the square of i – the idea is that we do not need to loop though the numbers more than the square root of the max value, so checking for i squared is as far as we need to go.
Posted in Lab49
Java coder in a C# world
As an initial blog, I thought I would start out by writing about the stuff I’ve been doing over the past few months. Having been a Java coder for a number of years, I have recently been introduced to the C# .NET world – mainly because of the requirements of a project.
This has highlighted a few issues for me. Java being a wonderful language lacks some of the cool features that are provided when working with C#. I won’t go into any details, however, to name but a few of the features i’ve come across are: Lambda expressions(=>), LINQ Queries, and the Reactive Extension framework (Rx). Lambda Expressions http://msdn.microsoft.com/en-us/library/bb397687.aspx, are anonymous functions that have an expression and a statment, and can provide for some very concise code especially when working with the Rx framework subscriptions. Also, working with a load of C# guys, they were wondering how Java developers can get away without having a query language like LINQ http://msdn.microsoft.com/en-us/netframework/aa904594.aspx, which can be used to provided SQL like queries on collections for example. Reactive Extensions (Rx) are great for writing event based programs http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx, these provide for very fast non-locking event based development.
Another useful tip is to watch the Microsoft Channel9 videos (just search the internet for Microsoft Channel9). There are a number of great talks on a number of MS creations.
On the whole, working with C# is surprisingly intuitive (or not so, considering where it was derived from ) for a Java guy. But Microsoft seem to have the time and money to invest in Research which maybe Sun lacked. I’ve heard that Java 7 will have some cool features, but it seems that Java is playing catch-up at times in terms of it’s language maturity.
On another note, I’ve been looking at Scala http://www.scala-lang.org/. This is a great language which is more mature than Java, and gives you the benefits of functional programming with the ability to talk to standard Java code as well . Scala runs on the JVM and compiles to byte code and has some very powerful language features which I will leave you to read about.
Posted in Lab49
Hello world!
Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!
Posted in Uncategorized