Bypassing SSL in Apache Webservice Clients

Today, at work, I experienced a problem that has bitten me before while developing a client application that consumed webservices over SSL. Specifically, I was using the Apache Axis webservice library. In the past we did the "right" thing - we obtained the appropriate certificate(s), updated our truststore and keystore, and continued development. However, I wanted to find a way to keep developing without having to go through all those hoops. So, I started searching using google. And I found this forum post on Code Ranch that provided the answer I was looing for.

So basically, Apache Axis recognizes that developers may want to be able to consume webservices without having to deal with SSL, and they have a property specifically for this. The line I added to my code is below. However, I added a condition around it that checked whether the code was running in an IDE, as opposed to on a server (we have a java flag we pass in to the server as an argument) - after all, it goes without saying that this code should…

Project Euler Problem 20 in Clojure

Today we are going to skip forward to problem 20 from Project Euler. The problem statement is:

n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!

The first thing we need is a function that calculates the factorial of a number. A simple function to do this is:

(defn factorial [n]
  (reduce * (range 1 (inc n))))

However, this function has 2 problems:

  1. 100! is going to be an extremely large number, so we are going to need to use clojure's ability to use arbitrarily large numbers (using 1M, and passing in 100M as the argument).
  2. The code does not take into account the previously calculated factorials. This is fine for most cases, but in this case we want to improve the efficiency of our code because of the large numbers we are dealing with.

The first problem is easily resolved using clojure's ability to use arbitrarily large numbers, as…

Project Euler Problem 12 in Clojure

If we were to continue solving the Project Euler problems in order, we would expect to work on problem 11 - problem 10 was solved here. But that problem is a little tricky, and I need to wrap my head around it. So we shall skip ahead to problem 12 instead.

The problem statement for problem 12 is:

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?

The first thing we need to do is write a function that generates triangle numbers. This seems like a perfect job for corecursion! If you need a refresher, read this…

Project Euler Problem 9 in Clojure

Today, we shall solve problem 9 from Project Euler. The problem statement is:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

I want to solve this using for comprehensions. The code I came up with is:

(defn euler-9 []
  (for [x (range 1 1000)
        y (range (inc x) 1000)
        z (range (inc y) 1000)
        :when (and (= (+ (* x x)
                         (* y y))
                      (* z z))
                   (= 1000
                      (+ x y z)))]
    (* x y z)))

What this does is cycle through every triplet combination of integers upto 1000 and, when the triplet combination is both a pythagorean triplet and add up to 1000, returns the product of the triplet.

And there you have it, problem 9 is solved.

Project Euler Problem 8 in Clojure

We solved problem 7 from Project Euler in this post. So, today we shall solve problem 8. The problem statement is:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.