(define (even x) (= (modulo x 2) 0))
(define (twice x) (* x 2))
(define (half x) (/ x 2))
(define (rfmult a b)
(cond ((= 0 a) 0)
((= 0 b) 0)
((even a) (twice (rfmult (half a) b)))
(else (+ b (twice (rfmult (half (- a 1)) b))))))
I've come to the understanding that (rfmult 3 4) is called, the else statement is triggered and after that (- 3 1) takes place of a and is cut in half so then it becomes (rfmult 1 4). At this point, I get lost because if it was multiplied by 2, it would never end. I just can't seem to make sense of it in my head.
The recursion ends at the 'base cases' (where there is no recursive call). Your bases cases are a or b is 0.
Use 'trace-define'
|(rfmult 3 4)
| (rfmult 1 4)
| |(rfmult 0 4) ;; ends here
| |0
| 4
|12
like this:
(trace-define (rfmult a b) ; <= here
(cond ((= 0 a) 0)
((= 0 b) 0)
((even a) (twice (rfmult (half a) b)))
(else (+ b (twice (rfmult (half (- a 1))b))))))
I think I figured it out...... so lets call (rfmult 100 5)
This would then call (rfmult (100/2 5)
(50/2 5)*2
((25-1)/2 5) *2
(12/2 5) * 2 + b
(6 5)*2
(3 5)*2
((3-1)/2 5) *2
(1 5)*2 + b
0!
Then you track upwards through the recursion.
So, in the (1 5) block the b value becomes 15 because 5*2 + 5=15
Then, the (3 15) block b becomes 15 *2 = 30
Then, (6 30) b becomes 30 * 2 = 60
Then, (12 60) 60*2 + 5 = 125
(25 125) 125 * 2 => 250
which brings us back to the first call of (50 250) where 250*2 = 500 and that is the solution of 5*100...
If this is the wrong thought process please correct me! I've been sitting on this recursive structure for about 2 hours now and am excited to see it sort of make sense!
Related
I am trying to go through SICP using Racket and Project Euler as reference for practice. This question is in regards to problem 4 (https://projecteuler.net/problem=4), hence:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
I am currently still on Chapter 1 (SICP) and loops have not been covered yet, as I see most solutions use. My code runs out of memory every time I try to execute (I have been stuck on this for hours - https://github.com/polila/Project-Euler/commits?author=polila&since=2018-03-19T04:00:00Z&until=2018-03-20T04:00:00Z) LOL.
What I am trying to do is use a recursive procedure by the process of linear iteration (at least that's what I think) - so in my mind it was something like this, where a and b are the max limit of n-digit:
(largest-palindrome a b max)
( 999 999 0)
( 999 998 0)
( 999 997 0)
( . . .)
( . . .)
( 999 99 x)
( 998 998 x)
( 998 997 x)
( 998 996 x)
( . . x)
( 100 100 x)
I realize that I wouldn't have to go all the way to the minimum range but even for small test cases such as 2-digit palindromes with the range of [90,99] it still doesn't execute. But here is my code, where am I going wrong??????????
#lang racket
(define (exp a b)
(define (iter product count)
(if (= count b)
product
(iter (* a product) (+ count 1))))
(if (= b 0)
1
(iter a 1)))
(define (palindrome? ab)
(define (length ab)
(define (iter n count)
(if (= n 0)
count
(iter (floor (/ n 10)) (+ count 1))))
(iter ab 0))
(define (check-symmetry ab left right)
(if (> left right)
(if (= (floor (/ (modulo ab (exp 10 left)) (exp 10 (- left 1))))
(floor (/ (modulo ab (exp 10 right)) (exp 10 (- right 1)))))
(check-symmetry ab (- left 1) (+ right 1))
#f)
#t))
(check-symmetry ab (length ab) 1))
(define (largest-palindrome a b max)
(if (> b 100)
(if (and (palindrome? (* a b)) (< max (* a b)))
(largest-palindrome a (- b 1) (* a b))
(largest-palindrome a (- b 1) max))
(largest-palindrome (- a 1) (- a 1) (* a b)))
(if (< a 100)
max
0))
You have the right general idea of looking at multiples starting at 999x999 all the way down to 100x100. You can improve efficiency on this method by terminating b whenever a x b is a palindrome, such that you don't test for palindromes in a x b-1 etc. since those products will be smaller in value than a x b.
What this looks like is:
999 x 999 -- not palindrome
999 x 998 -- not palindrome
|
995 x 583 -- palindrome (if current palindrome < this, save this value)
994 x 994 -- not palindrome (Notice the jump here instead of testing 995 x 582)
994 x 993 -- not palindrome
|
994 x 100 -- not palindrome
993 x 993 -- not palindrome
|
993 x 913 -- palindrome (if current < this, save this value)
992 x 992 -- not palindrome
|
100 x 100 -- not palindrome (hit base case and return current palindrome)
In your implementation of largest-palindrome, the first if-statement, ie. (if (> b 100) ...), is dead code. Moreover, the recursive steps in that if statement also have no base case(s), hence why you run out of memory every time as a result of the infinite recursion.
You can simplify much of your code. For example, consider the following palindrome? predicate that checks whether a number is equal to its digits reversed:
(define (palindrome? n)
(define (in-reverse n acc)
(if (< n 10)
(+ (* acc 10) n)
(in-reverse (quotient n 10)
(+ (* acc 10)
(remainder n 10)))))
(= n (in-reverse n 0)))
ie.
(palindrome? 1001)
=> #t
(palindrome? 1010)
=> #f
You can then implement largest-palindrome using this predicate:
;; largest-palindrome: Finds the largest palindrome that is a
;; multiple of two numbers, each of which has n digits.
(define (largest-palindrome n)
(define low (expt 10 (sub1 n))) ;; if n = 3, low = 100
(define high (sub1 (expt 10 n))) ;; if n = 3, high = 999
(define (largest a b mymax low high)
(define prod (* a b))
(cond
((< a low) mymax)
((or (< b low)
(< prod mymax))
(largest (sub1 a) (sub1 high) mymax low (sub1 high)))
((and (> prod mymax)
(palindrome? prod))
(largest (sub1 a) (sub1 high) prod b (sub1 high)))
(else
(largest a (sub1 b) mymax low high))))
(largest high high 0 low high))
this works for various values of n:
(largest-palindrome 3)
=> 906609
(largest-palindrome 5)
=> 9966006699
(largest-palindrome 7)
=> 99956644665999
I'm trying to create a Division function using only subtraction. What I have so far is enough to handle positive numbers. What keeps tricking me up is handling it for negative numbers. I can go ahead and just grab the absolute value of x and y and it works perfectly, but then my answer can never be negative. Anyone here whose had to do something similar before?
(define Divide (lambda (a b c)
(if (> a 0)
(Divide (- a b) b (+ c 1))
c
)
)
)
You can assign the product of sign values of a and b to a variable, then deal with only absolute values of both a and b while doing the recursion. Output then becomes the product of c and the sign variable as (* c sign). Consider the following:
(define (divide num denom)
(let div ([n num]
[d denom]
[acc 0]
[sign 1])
(cond
[(< n 0)
(div (- n) d acc (- sign))]
[(< d 0)
(div n (- d) acc (- sign))]
[(< n d)
(* sign acc)]
[else
(div (- n d) d (add1 acc) sign)])))
For example,
> (divide 10 7)
1
> (divide -10 7)
-1
> (divide -10 -7)
1
> (divide 10 -7)
-1
Note that if you use the condition (if (> a 0) ... instead of (if (>= a b) ..., then you add an extra step in your recursion, which is why using your function, (Divide 10 7 0) outputs 2.
In cases like this you often want to define an auxiliary function that the main function calls after massaging the data:
(define (Divide a b)
(define (go a b c)
(if (> a 0)
(go (- a b) b (+ c 1))
c))
(cond
[(and (> a 0) (> b 0))
(go a b 0)]
[(and (< a 0) (< b 0))
(go (- a) (- b) 0)]
[(< a 0)
(- (go (- a) b 0))]
[(< b 0)
(- (go a (- b) 0))]))
The following function was given to me on a review sheet:
(define mystery(lambda(m n)
(cond
((= m 0) n)
((= n 0) m)
(#t (+ 2(mystery(- m 1)(- n 1))))
)))
The first two conditions are simple, it's just the recursive otherwise that's confusing me. It just seems to me that the recursion will continue until they both equal zero, which certainly doesn't return the sum. Can someone provide an explanation?
First, let's format the code a bit better to see what's happening:
(define (mystery m n)
(cond ((= m 0) n)
((= n 0) m)
(else (+ 2 (mystery (- m 1) (- n 1))))))
Now, remember that a cond executes only the action corresponding to the first condition that is true (from top to bottom), the others are ignored. If none of the conditions is true, then the else part is executed. The important thing to remember is that only one action is executed.
In particular, your mystery procedure will stop when either m or n becomes zero, not when both become zero. When one of the two reaches zero, the recursion starts to unwind, returning the sum. You can see this when tracing the execution - for example, in Racket:
(require racket/trace)
(trace mystery)
(mystery 3 2)
>(mystery 3 2)
> (mystery 2 1)
> >(mystery 1 0)
< <1
< 3
<5
Just to elaborate on Óscar López's answer (I can't format this in a comment): I find that it's often useful to write these sorts of little recursive maths functions down as if they were maths:
Let m and n be natural numbers, then
n + m = n if m = 0;
n + m = m if n = 0;
n + m = n - 1 + m - 1 + 2;
there are no other cases.
I feel the best way is not to nest but to precompute. Looking at the base case we test with either zero:
(mystery 0 2) ; ==> 2
(nystery 3 0) ; ==> 3
Thus every time at least one argument is zero it returns the other argument. Lets try with a non zero value and remember the second you see a value we have already done before you just switch it with its result:
(mystery 1 3) ; ==
(+ 2 (mystery 0 2)) ; == (we switch known value)
(+ 2 2)
; ==> 4
(mystery 4 1) ; == (we substitute with the expression)
(+ 2 (mystery 3 0)) ; == (we switch known value)
(+ 2 3)
; ==> 5
Since we know the base case always returns the other value we don't need to precalculate it. Here is a go that does that:
(mystery 3 9) ; == (we substitute with the expression)
(+ 2 (mystery 2 8) ; == (we substitute with the expression)
(+ 2 (+ 2 (mystery 1 7))) ; == (we substitute with the expression)
(+ 2 (+ 2 (+ 2 (mystery 0 6))) ; == (we substitute with the expression, n, which is 6)
(+ 2 (+ 2 (+ 2 6))) ; == (we substitute (+ 2 6))
(+ 2 (+ 2 8)) ; == (we substitute (+ 2 8))
(+ 2 10) ; == (we substitute (+ 2 10)
; ==> 12
We can generalize what will happen. The lowest of n and m will decide when the recursion ends. At each step it will add 2 and recurse. Thus it is a fancy way of making:
(define (double-min n m)
(let ((vmin (min n m))
(vmax (max n m)))
(+ (* 2 vmin) (- vmax vmin))))
Which again is a fancy way of adding the two numbers since if n > m, then 2*m+(n-m) = m+m+(n-m) = m+n
(define mystery(lambda(m n)
(cond
((= m 0) n)
((= n 0) m)
(#t (+ 2 (mystery (- m 1) (- n 1))))
)))
First and second conditions are obvious.
Explanation of how the third statement is working:
1 each is taken out of m and n and kept as 2 outside this function.
This is continued till either m is 0 or n is 0.
The first 2 cases are obvious, the sum of 2 numbers where one of the numbers is 0, is equal to the other number.
In the last case, after checking the arguments for 0, we know for sure that both of them are non-0. Assuming that mystery returns the sum of its 2 arguments, then
(+ 2 (mystery (- arg1 1) (- arg2 1)))
will return a sum that is equal to (mystery arg1 arg2) and will eventually halt when one of the arguments is 0, returning the desired result.
Assuming that mystery returns the sum of its 2 arguments is key here and is called the recursive leap of faith. (Google it)
I want to define a function that takes an input "n" (the number of variables) and return all possible truth values. Here, I represent the truth values for a variable i (1 <= i <= n) with +i representing true, and -i representing false.
For example:
(generate-values 2)
should return:
((2 1)(2 -1)(-2 1)(-2 -1))
(generate-values 3)
should return:
((3 2 1)(3 2 -1)(3 -2 1)(3 -2 -1)(-3 2 1)(-3 2 -1)(-3 -2 1)(-3 -2 -1))
Here is my incorrect attempt:
(defun generate-values (n)
(cond
((equal n 0) nil)
(t (list (cons n (generate-values (- n 1)))
(cons (- 0 n) (generate-values (- n 1)))))))
I know why this is incorrect, but I am not able to find a way to generate (3 2 1) and then move on to (3 2 -1). My program outputs:
((3 (2 (1) (-1)) (-2 (1) (-1))) (-3 (2 (1) (-1)) (-2 (1) (-1))))
Any help with this question qould be thoroughly appreciated! Thanks!
It might be easiest to approach this in the easiest way possible, and then to figure out how to make it a bit simpler or more efficient afterward.
If you're doing this recursively, it's important to consider what the bases cases are. A reasonable base case here is probably when n = 0. The function is always supposed to return a list of lists. In the n = 0 case, there are no "variables", so the result has to be a list of the empty list: (()).
For the case that n is anything else, consider what the function returns for n-1. It's a list of all the combinations on n-1 "variables". All you need to do is prepend n to each of those, and prepend -n to each of those, and then make sure you end up with a list of all of those.
Encoding that directly, we end up with something like this:
(defun table (n)
(if (zerop n)
'(())
(let* ((table (table (1- n)))
(plus-pos-n (mapcar (lambda (subtable)
(list* n subtable))
table))
(plus-neg-n (mapcar (lambda (subtable)
(list* (- n) subtable))
table)))
(nconc plus-pos-n plus-neg-n))))
CL-USER> (table 3)
((3 2 1) (3 2 -1) (3 -2 1) (3 -2 -1) (-3 2 1) (-3 2 -1) (-3 -2 1) (-3 -2 -1))
Now, let's look at what your current implementation is doing differently, noting that it doesn't have to be exactly the same algorithm, of course.
(defun generate-values (n)
(cond
((equal n 0)
nil)
(t
(list (cons n
(generate-values (- n 1)))
(cons (- 0 n)
(generate-values (- n 1)))))))
Stylistically, since there are only two branches, I'd prefer if to cond here, but that's not a problem. Before attacking the base case, lets look at the recursive case, when n ≠ 0. First, you're calling generate-values twice; it would be more efficient to call it once and save the result. That could end up being important later if you're calling this function with big values of n, but it doesn't make the function incorrect. But remember what generate-values returns; it returns a list of the different combinations. That means that your call to (cons n (generate-values …)) is returning a list whose first element is n, and whose remaining elements are the combinations for n-1. E.g., you're doing something like:
CL-USER> (table 1)
((1) (-1))
CL-USER> (cons 2 (table 1))
(2 (1) (-1))
But that's not what you want. You really want to add n to each of those lists:
CL-USER> (mapcar (lambda (x)
(cons 2 x))
(table 1))
((2 1) (2 -1))
That's the issue in the recursive case. There's an issue in the base case, too. In the recursive case, you want to add n and -n to each of the sublists from the n-1 case. So what happens when you have n = 1? You want to be getting (cons 1 '()) and (cons -1 '()). But since the second argument to cons is going to be each list inside of the result of (generate-values 0), you really need to have something in the list returned by (generate-values 0). What needs to be there? The empty list needs to be there. So the base case needs to return (()), not (). So, after making those changes, your code would be:
(defun generate-values (n)
(cond
((equal n 0)
'(()))
(t
(list (mapcar (lambda (x)
(cons n x))
(generate-values (- n 1)))
(mapcar (lambda (x)
(cons (- 0 n) x))
(generate-values (- n 1)))))))
CL-USER> (generate-values 3)
(((3 (2 (1)) (2 (-1))) (3 (-2 (1)) (-2 (-1))))
((-3 (2 (1)) (2 (-1))) (-3 (-2 (1)) (-2 (-1)))))
That's closer, but it's still not quite right. There's another in the recursive case. You end up generating the values that have n in the beginning (a list of them), and the values that have -n in the beginning (a list of them), but then you're using list to combine them. That returns a single list with two values. Instead, you want a single list that has the values from each of them. You want to combine them with append (or, since all the structure is newly generated, you could use nconc):
(defun generate-values (n)
(cond
((equal n 0)
'(()))
(t
(append (mapcar (lambda (x)
(cons n x))
(generate-values (- n 1)))
(mapcar (lambda (x)
(cons (- 0 n) x))
(generate-values (- n 1)))))))
CL-USER> (generate-values 3)
((3 2 1) (3 2 -1) (3 -2 1) (3 -2 -1) (-3 2 1) (-3 2 -1) (-3 -2 1) (-3 -2 -1))
This final implementation isn't exactly what I started with, but it's essentially the same in terms of the algorithm. The differences are mostly stylistic, but there are some efficiency concerns, too. Using nconc instead of append would save some memory, and it really would be good to cache the results from the recursive call, rather than recomputing it. Stylistic issues that don't affect correctness might be using if instead of cond, using list* instead of cons (to indicate that we're working with lists, not trees of cons cells), and it's nice to note that you don't have to do (- 0 n), - with a single argument returns the argument's negation. That is, (- n) = -n.
I'm a newbie in LISP. I'm trying to write a function in CLISP to generate the first n numbers of Fibonacci series.
This is what I've done so far.
(defun fibonacci(n)
(cond
((eq n 1) 0)
((eq n 2) 1)
((+ (fibonacci (- n 1)) (fibonacci (- n 2))))))))
The program prints the nth number of Fibonacci series. I'm trying to modify it so that it would print the series, and not just the nth term.
Is it possible to do so in just a single recursive function, using just the basic functions?
Yes:
(defun fibonacci (n &optional (a 0) (b 1) (acc ()))
(if (zerop n)
(nreverse acc)
(fibonacci (1- n) b (+ a b) (cons a acc))))
(fibonacci 5) ; ==> (0 1 1 2 3)
The logic behind it is that you need to know the two previous numbers to generate the next.
a 0 1 1 2 3 5 ...
b 1 1 2 3 5 8 ...
new-b 1 2 3 5 8 13 ...
Instead of returning just one result I accumulate all the a-s until n is zero.
EDIT Without reverse it's a bit more inefficient:
(defun fibonacci (n &optional (a 0) (b 1))
(if (zerop n)
nil
(cons a (fibonacci (1- n) b (+ a b)))))
(fibonacci 5) ; ==> (0 1 1 2 3)
The program prints the nth number of Fibonacci series.
This program doesn't print anything. If you're seeing output, it's probably because you're calling it from the read-eval-print-loop (REPL), which reads a form, evaluates it, and then prints the result. E.g., you might be doing:
CL-USER> (fibonacci 4)
2
If you wrapped that call in something else, though, you'll see that it's not printing anything:
CL-USER> (progn (fibonacci 4) nil)
NIL
As you've got this written, it will be difficult to modify it to print each fibonacci number just once, since you do a lot of redundant computation. For instance, the call to
(fibonacci (- n 1))
will compute (fibonacci (- n 1)), but so will the direct call to
(fibonacci (- n 2))
That means you probably don't want each call to fibonacci to print the whole sequence. If you do, though, note that (print x) returns the value of x, so you can simply do:
(defun fibonacci(n)
(cond
((eq n 1) 0)
((eq n 2) 1)
((print (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))))
CL-USER> (progn (fibonacci 6) nil)
1
2
1
3
1
2
5
NIL
You'll see some repeated parts there, since there's redundant computation. You can compute the series much more efficiently, however, by starting from the first two numbers, and counting up:
(defun fibonacci (n)
(do ((a 1 b)
(b 1 (print (+ a b)))
(n n (1- n)))
((zerop n) b)))
CL-USER> (fibonacci 6)
2
3
5
8
13
21
An option to keep the basic structure you used is to pass an additional flag to the function that tells if you want printing or not:
(defun fibo (n printseq)
(cond
((= n 1) (if printseq (print 0) 0))
((= n 2) (if printseq (print 1) 1))
(T
(let ((a (fibo (- n 1) printseq))
(b (fibo (- n 2) NIL)))
(if printseq (print (+ a b)) (+ a b))))))
The idea is that when you do the two recursive calls only in the first you pass down the flag about doing the printing and in the second call instead you just pass NIL to avoid printing again.
(defun fib (n a b)
(print (write-to-string n))
(print b)
(if (< n 100000)
(funcall (lambda (n a b) (fib n a b)) (+ n 1) b (+ a b)))
)
(defun fibstart ()
(fib 1 0 1)
)