For example, the extended euclidean algorithm (quoted from wiki):
function extended_gcd(a, b)
x := 0 lastx := 1
y := 1 lasty := 0
while b ≠ 0
quotient := a div b
(a, b) := (b, a mod b)
(x, lastx) := (lastx - quotient*x, x)
(y, lasty) := (lasty - quotient*y, y)
return (lastx, lasty)
which I tried and got:
(defn extended-gcd
[a b]
(loop [a a b b x 0 y 1 lx 1 ly 0]
(if (zero? b)
[lx ly]
(recur b (mod a b)
(- lx (* (int (/ a b)) x))
(- ly (* (int (/ a b)) y))
x y))))
I guess I could find a way to translate loops that deal with sequence. But how about this one? How do I write it in clojure way? something with map, reduce, etc. rather than loop recur.
For the extended Euclidean algorithm you can use a simple recursion, which makes a function look quite elegant:
(defn extended-gcd [a b]
(if (zero? b) [1 0]
(let [[q r] [(quot a b) (rem a b)]
[s t] (extended-gcd b r)]
[t (- s (* q t))])))
Let's try it:
user=> (extended-gcd 120 23)
[-9 47]
Not all problems need to be solved by using map/reduce/sequence. I would argue that the above is just as Clojure way as a "(reduce + [1 2 3 4 5])" type of an answer you are looking for.
For this kind of problem iterate is often a good alternative to using loop. In this case it leads to a fairly transparent translation of the source algorithm:
(defn extended-gcd [a b]
(->> {:a a, :b b, :x 0, :y 1, :lx 1, :ly 0}
(iterate
(fn [{keys [a b x y lx ly]}]
(let [q (quot a b)]
{:a b, :b (mod a b), :x (- lx (* q x)), :lx x, :y (- ly (* q y)), :ly y})))
(drop-while #(not= 0 (:b %)))
first
((juxt :lx :ly))))
That said, using loop is a Clojure way too -- admonitions to avoid it, I believe, are meant to encourage use of higher-level constructs where they're more appropriate.
Related
This is a repeat of this question: Calculate primes p and q from private exponent (d), public exponent (e) and the modulus (n)
I'm just explicitly stating the problem and asking for a solution - hopefully in clojure:
public key (n):
8251765078168273332294927113607583143463818063169334570141974734622347615608759376136539680924724436167734207457819985975399290886224386172465730576481018297063
private key (d):
3208816897586377860956958931447720469523710321495803767643746679156057326148423456475670861779003305999429436586281847824835615918694834568426186408938023979073
exponent (e): 65537
and I want to get the seeds: p and q
p: 87270901711217520502010198833502882703085386146216514793775433152756453168234183
q: 87270901711217520502010198833502882703085386146216514793775433152756453168234183
To get n and d in the first place is not too hard:
(defn egcd [a b]
(if (= a 0)
[b, 0, 1]
(let [[g y x] (egcd (mod b a) a)]
[g (- x (* y (quot b a))) y])))
(defn modinv [a m]
(let [[g y x] (egcd a m)]
(if (not= 1 g)
(throw (Exception. "Modular Inverse Does Not Exist"))
y)))
(def n (* p q))
(def d (modinv e (* (dec p) (dec q)))
Now I require a reverse transform.
The algorithm Thomas Pornin posted in response to the question you link to works perfectly. Transcribed into Clojure, it looks like this:
;; using math.numeric-tower 0.0.4
(require '[clojure.math.numeric-tower :as num])
(defn find-ks [e d n]
(let [m (num/round (/ (*' e d) n))]
((juxt dec' identity inc') m)))
(defn phi-of-n [e d k]
(/ (dec' (*' e d)) k))
(defn p-and-q [p+q pq]
[(/ (+' p+q (num/sqrt (-' (*' p+q p+q) (*' 4 pq)))) 2)
(/ (-' p+q (num/sqrt (-' (*' p+q p+q) (*' 4 pq)))) 2)])
(defn verify [n p q]
(== n (*' p q)))
(defn solve [e d n]
(first
(for [k (find-ks e d n)
:let [phi (phi-of-n e d k)
p+q (inc' (-' n phi))
[p q] (p-and-q p+q n)]
:when (verify n p q)]
[p q])))
Applying this to your e, d and n we get
(solve 65537N 3208816897586377860956958931447720469523710321495803767643746679156057326148423456475670861779003305999429436586281847824835615918694834568426186408938023979073N 8251765078168273332294927113607583143463818063169334570141974734622347615608759376136539680924724436167734207457819985975399290886224386172465730576481018297063N)
;= [94553452712951836476229946322137980113539561829760409872047377997530344849179361N
87270901711217520502010198833502882703085386146216514793775433152756453168234183N]
You posted the same number as p and q, by the way -- the second one in the result vector above -- but it's easy to verify that these are the correct numbers by using the pair to rederive n and d.
How could I convert this:
[a b c d e]
or this:
(e d c b a) ;(rseq [a b c d e])
to this:
[a[b[c[d[e]]]]]
I've been wracking my brain and I feel like there is a simple solution! :-\
Ultimately I want to do this:
[a b c d e]
[a b c x y]
[a b c d j k]
as this:
{a {b {c {d {e}
{j {k}}
{x {y}}}}
Which I think conj will help with
(Update: added answer to the new question added in the edit below the answer to the original question.)
I've actually answered this very question in #clojure recently.
Here are two approaches: f is pretty much the spec directly transformed into code, which however creates a seq -- (next xs) -- which immediately gets poured into a new vector at each step; g is a much better version which only allocates objects which will actually occur in the output, plus a vector and the seq links to traverse it:
;; [1 2 3] -> [1 [2 [3]]]
;; naive, quadratic:
(defn f [xs]
(if (next xs)
[(first xs) (vec (f (next xs)))]
(vec xs)))
;; only allocates output + 1 vector + a linear number of seq links,
;; linear overall:
(defn g [v]
(reduce (fn [acc x]
[x acc])
[(peek v)]
(rseq (pop v))))
NB. I'm overlooking the usual logarithmic factors arising from vector operations (so this is soft-O complexity).
As for producing a nested map, the above isn't particularly useful. Here's one approach:
(defn h
([v]
(h nil v))
([m v]
(assoc-in m v nil)))
(h [1 2 3 4])
;= {1 {2 {3 {4 nil}}}}
(def data
'[[a b c d e]
[a b c x y]
[a b c d j k]])
(reduce h {} data)
;= {a {b {c {x {y nil}, d {j {k nil}, e nil}}}}}
I'm using nil as a "terminator", since {y} (as currently found in the answer text) is not a well-formed literal. true might be a more convenient choice if you plan to call these maps as functions to check for presence of keys.
Simpler solution here (using destructuring and non-tail recursion):
http://ideone.com/qchXZC
(defn wrap
([[a & as]]
(if-let [[b & cs] as]
[a (wrap as)]
[a])))
I have a sequence:
[a b c ...]
And a function (f x y). I want to get this:
(f c (f b (f a 1)))
Etc.. How to do this?
Reduce, with a small adaptation:
(reduce #(f %2 %1) 1 [a b c])
(reduce (fn [acc x] (f x acc)) 1 [a b c d])
Suppose you have three functions of arity 1, 2 and 3 as below:
(defn I [x] x)
(defn K [x y] x)
(defn S [x y z] (x z (y z)))
Does clojure have an evaluation function or idiom for evaluating:
(I K S I I) as (I (K (S (I (I)))))
returning a parital function of arity 2?
I am considering creating a macro that can take the simple function definitions above and expand them to multi-arity functions that can return partial results. I would not want to create the macro if there is already a built in or idiomatic way to accomplish this.
Here is what the expanded macros would like for the above functions:
(defn I
([x] I x)
([x & more] (apply (I x) more)))
(defn K
([x] (partial K x))
([x y] x)
([x y & more] (apply (K x y) more)))
(defn S
([x] (partial S x))
([x y] (partial S x y))
([x y z] (x z (y z)))
([x y z & more] (apply (S x y z) more)))
I'm not sure I fully understand what you are trying to do, but the comp function is useful for doing this kind of "function chaining" you seem to be talking about. For example:
user> ((comp vec rest list) 1 2 3 4 5)
=> [2 3 4 5]
Which is equivalent to:
user> (vec (rest (list 1 2 3 4 5)))
=> [2 3 4 5]
In your case, if you have the list (I K S I I), and you want to evaluate it as (I (K (S (I (I))))), I would use (reduce comp ...), but you could also use (apply comp ...).
user> ((reduce comp [vec rest list]) 1 2 3 4 5)
=> [2 3 4 5]
user> ((apply comp [vec rest list]) 1 2 3 4 5)
=> [2 3 4 5]
You may also be interested in the -> or ->> macros. These macros nest their arguments sequentially into the next arguments. The -> macro will nest into the first position of the next expression, whereas the ->> macro will nest into the last position of the next expression. If the "next thing" is a function, both will behave the same, and form an expression of (function nested-things-so-far), and continue along.
Really, examples are best:
(-> 1 (+ 10) (- 100) inc)
;//Expands to...
(inc (- (+ 1 10) 100))
;//Evaluating in the REPL...
user> (-> 1 (+ 10) (- 100) inc)
=> -88
(->> 1 (+ 10) (- 100) inc)
;//Expands to...
(inc (- 100 (+ 10 1)))
;//Evaluating in the REPL...
user> (-> 1 (+ 10) (- 100) inc)
=> 90
However, it seems more like you want to do something involving auto-currying (although, again, I don't think I fully understand), and for that I don't know of anything pre-existing built-in way.
I'm trying to work through some of the exercises in SICP using Clojure, but am getting an error with my current method of executing Simpson's rule (ex. 1-29). Does this have to do with lazy/eager evalution? Any ideas on how to fix this? Error and code are below:
java.lang.ClassCastException: user$simpson$h__1445 cannot be cast to java.lang.Number
at clojure.lang.Numbers.divide (Numbers.java:139)
Here is the code:
(defn simpson [f a b n]
(defn h [] (/ (- b a) n))
(defn simpson-term [k]
(defn y [] (f (+ a (* k h))))
(cond
(= k 0) y
(= k n) y
(even? k) (* 2 y)
:else (* 4 y)))
(* (/ h 3)
(sum simpson-term 0 inc n)))
You define h as a function of no arguments, and then try to use it as though it were a number. I'm also not sure what you're getting at with (sum simpson-term 0 inc n); I'll just assume that sum is some magic you got from SICP and that the arguments you're passing to it are right (I vaguely recall them defining a generic sum of some kind).
The other thing is, it's almost always a terrible idea to have a def or defn nested within a defn. You probably want either let (for something temporary or local) or another top-level defn.
Bearing in mind that I haven't written a simpson function for years, and haven't inspected this one for algorithmic correctness at all, here's a sketch that is closer to the "right shape" than yours:
(defn simpson [f a b n]
(let [h (/ (- b a) n)
simpson-term (fn [k]
(let [y (f (+ a (* k h)))]
(cond
(= k 0) y
(= k n) y
(even? k) (* 2 y)
:else (* 4 y))))]
(* (/ h 3)
(sum simpson-term 0 inc n))))