Advertisement
flalli

How to find the function f that satisfies this

Oct 22nd, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.70 KB | None | 0 0
  1. ; The original problem:
  2.  
  3. ; I want to find out the fn that satisfies this test for any x > 0 and y > 0, supposing x are generally (but not guaranteed) aprox. y.
  4.  
  5. (define ok (lambda (fn x y)
  6.              (let ((result1 (fn x y))
  7.                    (result2 (/ 1 (fn (/ 1 x)
  8.                                      (/ 1 y)))))
  9.              (and (= result1 result2)
  10.                   (< (min x y) result1)
  11.                   (> (max x y) result1)))))
  12.  
  13. ; So I created this question: http://math.stackexchange.com/questions/985506/how-to-find-the-function-f-that-satisfies-fx-y-fx-1-y-1-1-a
  14.  
  15. ; Then I defined:
  16.  
  17. (define fn-solution (lambda (x y)
  18.                        (sqrt (* x y))))
  19.  
  20. ; But:
  21.  
  22. (ok fn-solution 5 8) => #f
  23.  
  24. ; for example. Why did not work? Because sqrt is not precise, it is a irrational number.
  25.  
  26. ; So, if x and y are exact numbers, then this solution works:
  27.  
  28. (define fn-final-solution
  29.   (lambda (x y)
  30.     (let ((use-inverse (< (min x y)
  31.                           (min (/ 1 x) (/ 1 y)))))
  32.       (if (not use-inverse)
  33.           (inexact->exact (sqrt (* x y)))
  34.           (/ 1 (inexact->exact (sqrt (* (/ 1 x)
  35.                                         (/ 1 y)))))))))
  36.  
  37. ; The final test:
  38.  
  39. (define nice-random
  40.   (lambda ()
  41.     (inexact->exact (/ (random) (random)))))
  42.  
  43. (let next ((x (nice-random))
  44.            (y (nice-random))
  45.            (counter 0))
  46.   (or (> counter 100000)
  47.       (let ((result (ok fn-final-solution x y)))
  48.         (and result
  49.              (next (nice-random)
  50.                    (nice-random)
  51.                    (+ counter 1))))))
  52.  
  53. ; Nice random is just to pick a random number sometimes less than 0 and sometimes bigger. Also it is exact number.
  54.  
  55. This test returns #t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement