~arun

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…

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…

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…

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.

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.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
242190226710556263211111093705442…``````