Clojure-Project Euler part 2

In a previous article (Clojure with Project Euler, part 1) I described the first 3 tasks from project Euler using clojure is a relatively young language, but having famous parents.
In this article, I will continue to familiarize the reader (and yourself) with this fun Java-Lisp. And show solution 3 simple problems of Euler.


Task 4


Job
Find the maximum product of 2 three-digit numbers, which is a palindrome.

Algorithm
1. Create POS-th all works.
2. To filter, leaving only the palindromes.
3. To find the maximum.

Code
We first define q-tion that defines a palindrome string or not.
I decided to train will power and try to write code with comments, at least f-tsii.
(defn palindrom?
"Returns true if x is palindrom
false otherwise."

[x]
(let [s (str x)]
(= (seq s) (reverse s))))

It is worth noting 4 things.
1. Description f-tsii it is written after the name and before the argument list. This description will be displayed when you call (doc your-function).
2. In clojure all the predicates (I understand them as f-tion of one variable, returns true or false) typically end in a question mark.
3. Since we pass the number, you have to convert it to a string first, this is done with the help f-tsii str.
4. word is of type String, and (reverse word) already returns a sequence and a simple comparison will always be false. Therefore, it is necessary to make from word is also POS-th. This we do by using (seq word).

Now the main part:
(reduce max
(palindrom filter?
(for [ i (range 100 1000) j (range i 1000)] (* i j))))


(Reduce max... ) should be more or less clear, simply takes the maximum from POS-ti that she receives.
(Filter... ) we filtered POS-th numbers and leave only the palindromes, and at once pass on to our predicate palindrom?, not anonymous f-tion, as in previous times.
(for [ i (range 100 1000) j (range i 1000)] (* i j))
it uses f-tsiya for that returns the POS-th. It specifies the local variables in brackets [] are usually used in loops (don't know how to call them properly, but I think everyone knows where i and j are used). The POS-t is formed from the values of the expression standing after binding [ ]. In our case, it is simply the product of i and j. There is still a slight improvement in the fact that j runs more than 100 to 1000, and the current i, which speeds up the work.
A vivid example for:
(for [i (range 1 5) j (range i 5)] [i j])
; Get POS-th vectors: ([1 1] [1 2] [1 3] [1 4] [2 2] [2 3] [2 4] [3 3] [3 4] [4 4])


Task 5


Job
Output the minimal number that is divisible by all numbers from 1 to 20, inclusive.

Algorithm
Take and find the least common multiple of 20 numbers.
Code
Of course you can write your NOC, but we don't... Better to use a ready-made f-tions, but rather by libraries. Usually together with clojure connect directly and clojure-contrib (http://clojure.org/libraries) — build libraries written, as I understand it, ordinary users. Clojure-contrib is simply in a jar file and included in the path along c clojure.jar. There are all sorts of different Goodies. It can be noted that the library lazy-seq is POS-th Fibonacci and POS-th Prime numbers, which we wrote in the last article. But now we will be interested in the math library, which has f-tsiya lcm — knock.
Plug-in:
(use 'clojure.contrib.math)

And solve:
(reduce lcm (range 1 21))


I'll skip some of the tasks that do not require any special new knowledge of the language, but only the implementation. So go straight to 8:

Task 8


Job
This number of 1000 marks and you should find it in the 5 consecutive numbers which give the maximum product and display this work. The number itself can be found here: (http://projecteuler.net/index.php?section=problems&id=8)

Algorithm
1. First, we need this number to enter into the program, I decided to save it to a file and read from there, because in recording his editor is not very convenient. At the same time together with the libraries the Java.
2. Split the number at the POS-th groups of 5 digits.
3. Multiply and take the maximum.

Code
The number (20 lines of 50 characters) is stored in a file input.txt
(def number
(let [reader (new java.io.BufferedReader (new java.io.FileReader "input.txt"))]
(reduce str (for [i (range 20)] (.readLine reader)))))

Here you can see the familiar classes BufferedReader and FileReader. To create new instances of a class using f-new tion in which the second parameter is the class name, third, fourth, etc. arguments to the constructor of this class. It is worth noting that we can import the class, not to write the full name, but to import all classes from a package, as in Java, you can't. Have one.
To invoke methods on the instance of the class through a point: (.instance methodName arg1 arg2 ...).

Write a method that will convert a string into POS-th digits:
(defn to-digit-seq
"Converts string st
to a sequence of digits."

[st]
(map #(Character/getNumericValue %) st))

It is worth noting that static methods and variables of Java classes can be referenced via the ClassName/methodName.

Now the main part:
(reduce max
(map #(reduce * %)
(partition 5 1 (to-digit-seq number))))

There is a funny f-tsiya partition that returns the POS-th subsequences of this pic-ti :)
In General, in our case, it broke the POS-th digits, POS-th whose elements are groups of 5 digits, with a step of 1.

That's probably all for now. Finally, I would like to give a link to a site, which collects a solution in Clojure code jobs from Euler, he suddenly stumbled. There really is not everything, but you can still watch what are options and to learn, think, http://clojure-euler.wikispaces.com/
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The use of Lisp in production

FreeBSD + PostgreSQL: tuning the database server

As we did a free Noodle for iOS and how we plan to earn