Tag Archives: code golf

Code Golf

CSLU did code golf today. I did 1 and a half tasks, which were:

  1. Output the first 100 prime numbers
  2. Output e to 100 decimal places

Prime numbers

I’m quite proud of this, I managed to do this in 55 characters initially but then after some collaboration with the rest of the club shrunk it down to 49 characters

2.upto(541){|a|i=2;i+=1 while a%i>0;p a if i==a}

e

I never got his fully working as I ended up getting caught up in list comprehensions. Ended up with:

1 + sum [1 / (product [m | m<- [1..n] ]) | n <- [1..300] ]

 

Which is the same as e = sum_{n=0}^{infty } frac{1}{n!} and shows how pretty Haskell is.