Iterative decompose-as-sum-of-squares in Racket - racket

I already tried this problem multiple times but can't seen to get it right. I want to write an iterative procedure decompose-as-sum-of-squares which inputs a positive integer n and outputs the first integer p such that p^2 + q^2 = n, where q^2 = (n - p^2), and neither p nor n - p^2 is 1. If such p does not exist, the function should return n.
A sample output is (dss 65)returns 4.
This is my code so far.
(define (dss n)
(define (sum-of-squares n)
(if (zero? n) 0
(cons (expt n 2)
(sum-of-squares (- n 1)))))
(sum-of-squares 1))
The output I get is
(dss 65) ; (1 . 0)
Which is clearly wrong. Please help!

Lists have nothing to do with this problem, the solution follows from the definition: you have to test all p values starting from 2 and see if we can find an integer q greater than 1 that satisfies the formula p^2 + q^2 = n. Something like this:
(define (dss n)
(define (sum-of-squares p)
(cond ((>= p n) n)
((let ((q (sqrt (- n (sqr p)))))
(and (integer? q) (not (= q 1))))
p)
(else (sum-of-squares (add1 p)))))
(sum-of-squares 2))
(dss 65)
=> 4

Related

Racket Programming ;Write a recursive function to compute the summation sum( x^2, x=1..n

;Write a recursive function to compute the summation sum( x^2, x=1..n)
;You must write a recursive function.
;If you use any iterative commands (for/loop/sum/etc you will receive a 0)
I'm stuck on this
(define (expo base x)
(cond ((or (= base 1) (= x 0)) 1)
(else (* base (expo base (- x 1)))))
I wrote this but its not right
Your code is working but is only missing a bracket at the end. You will see an error expected a ) to close ( possible cause: indentation suggests a missing ) when you are missing a bracket.
If n equal 5. You want see this: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55
#lang racket
(define (summation from-n to-n fn)
(local [(define (aux n)
(if (< n from-n)
0
(+ (fn n) (aux (- n 1)))))]
(aux to-n)))
(define (1^2+2^2+3^2+...+n^2 n)
(summation 1 n sqr))
(define (1+2+3+...+n n)
(summation 1 n identity))
;;; TEST
(1+2+3+...+n 100) ; 5050
(1^2+2^2+3^2+...+n^2 5) ; 55

Errors in Racket for SICP Exercise 1.11

The interpreter for Racket gives me errors
in my attempt to implement the recursive
function for Exercise 1.11:
#lang sicp
(define (f n)
(cond ((< n 3) n)
(else (+ f((- n 1))
(* 2 f((- n 2)))
(* 3 f((- n 3)))))))
(f 2)
(f 5)
The errors given by the Racket intrepreter are:
2
application: not a procedure;
expected a procedure that can be applied to arguments
given: 4
arguments...: [none]
context...:
/Users/tanveersalim/Desktop/Git/EPI/EPI/Functional/SICP/chapter_1/exercise_1-11.rkt: [running body]
As others noted, you're calling f incorrectly
Change f((- n 1)) (and other similar instances) to (f (- n 1))
(define (f n)
(cond ((< n 3) n)
(else (+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3)))))))
(f 2) ; 2
(f 5) ; 25

Return the sum of odd digits of a number

For class, I have to write a function that takes positive integer n and returns the sum of n’s odd digits in scheme. So far, I have my base case such that if n equals 0 then 0. But I am not sure on how to continue.
(define sumOddDigits
(lambda (n)
(if (= n 0)
0
Test cases:
(sumOddDigits 0) → 0
(sumOddDigits 4) → 0
(sumOddDigits 3) → 3
(sumOddDigits 1984) → 10
You could do it efficiently using one functional loop:
(define (sumOddDigits n)
(let loop ([n n])
(cond [(zero? n) 0]
[else
(let-values ([(q r) (quotient/remainder n 10)])
(+ (if (odd? r) r 0)
(loop q)))])))
One can get list of digits using following function which uses 'named let':
(define (getDigits n)
(let loop ((ol '()) ; start with an empty outlist
(n n))
(let-values (((q r) (quotient/remainder n 10)))
(if (= q 0) (cons r ol)
(loop (cons r ol) q)))))
Then one can apply a filter using odd? function to get all odd elements of list- and then apply 'apply' function with '+' to add all those elements:
(apply + (filter
(lambda(x)
(odd? x))
digitList))
Together following can be the full function:
(define (addOddDigits N)
(define (getDigits n)
(let loop ((ol '())
(n n))
(let-values (((q r) (quotient/remainder n 10)))
(if (= q 0) (cons r ol)
(loop (cons r ol) q)))))
(define digitList (getDigits N))
(println digitList)
(apply + (filter
(lambda(x)
(odd? x))
digitList)))
Testing:
(addOddDigits 156)
Output:
'(1 5 6)
6
Your basecase is if n < 10. Because you are then on the last digit.
You then need to check if it's odd, and if so return it. Else, return the addition qualifier(0).
If n > 10, you remainder off the first digit, then test it for odd.
If odd, then add it to a recursive call, sending in the quotient of 10(shaves off the digit you just added).
Else, you recursively call add-odds with the quotient of 10, without adding the current digit.
Here it is in a recursive form(Scheme LOVES recursion) :
(define add-odds
(lambda (n)
(if(< n 10)
(if(= (remainder n 2) 1)
n
0)
(if(= (remainder (remainder n 10) 2) 1)
(+ (remainder n 10) (add-odds (quotient n 10)))
(add-odds(quotient n 10))))))
First get a (reversed) list of digits with simple recursive implementation:
(define (list-digits n)
(if (zero? n) '()
(let-values ([(q r) (quotient/remainder n 10)])
(cons r (list-digits q)))))
then filter the odd ones and sum them:
(define (sum-of-odd-digits n)
(apply + (filter odd? (list-digits n))))
Note: (list-digits 0) returns '() but it is ok for later usage.
More accurate list-digits iterative implementation (produce list of digits in right order):
(define (list-digits n)
(define (iter n acc)
(if (zero? n) acc
(let-values ([(q r) (quotient/remainder n 10)])
(iter q (cons r acc)))))
(iter n '()))

how do i write this operation in racket?

How would I formulate the following operation in racket?
(n^2 + 300) (13/n)?
I got the first part done-
(define (f n)
( + ( * n n ) 300))
So If I type in (f 2) I would get 304.
But How do I add the second part of this equation (13/n)?
If you want to just use direct multiplication, this works:
(define (f n)
(* (+ (* n n) 300) (/ 13 n)))
If all you are going to do is square a number, then you could also do:
(define (f n)
(* (+ (sqr n) 300) (/ 13 n)))
And finally, if you needed to raise n to some power, then you could also do:
(define (f n)
(* (+ (expt n 2) 300) (/ 13 n)))
This is straightforward:
(define (f n)
(* (+ (* n n) 300) (/ 13 n)))

How would I express this Scheme function more clearly?

(define (repeated f n)
if (= n 0)
f
((compose repeated f) (lambda (x) (- n 1))))
I wrote this function, but how would I express this more clearly, using simple recursion with repeated?
I'm sorry, I forgot to define my compose function.
(define (compose f g) (lambda (x) (f (g x))))
And the function takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the nth repeated application of f.
I'm assuming that (repeated f 3) should return a function g(x)=f(f(f(x))). If that's not what you want, please clarify. Anyways, that definition of repeated can be written as follows:
(define (repeated f n)
(lambda (x)
(if (= n 0)
x
((repeated f (- n 1)) (f x)))))
(define (square x)
(* x x))
(define y (repeated square 3))
(y 2) ; returns 256, which is (square (square (square 2)))
(define (repeated f n)
(lambda (x)
(let recur ((x x) (n n))
(if (= n 0)
args
(recur (f x) (sub1 n))))))
Write the function the way you normally would, except that the arguments are passed in two stages. It might be even clearer to define repeated this way:
(define repeated (lambda (f n) (lambda (x)
(define (recur x n)
(if (= n 0)
x
(recur (f x) (sub1 n))))
(recur x n))))
You don't have to use a 'let-loop' this way, and the lambdas make it obvious that you expect your arguments in two stages.
(Note:recur is not built in to Scheme as it is in Clojure, I just like the name)
> (define foonly (repeat sub1 10))
> (foonly 11)
1
> (foonly 9)
-1
The cool functional feature you want here is currying, not composition. Here's the Haskell with implicit currying:
repeated _ 0 x = x
repeated f n x = repeated f (pred n) (f x)
I hope this isn't a homework problem.
What is your function trying to do, just out of curiosity? Is it to run f, n times? If so, you can do this.
(define (repeated f n)
(for-each (lambda (i) (f)) (iota n)))