Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. def mid(x,y)
  2. midpoint = (x + y) / 2.0
  3. end
  4.  
  5.  
  6. def prime(value,n)
  7. check = true
  8. num = sqrt(value,n)
  9. n = num.round
  10. start = 2
  11. while start <= n
  12. if value % start == 0
  13. check = false
  14. end
  15. start = start + 1
  16. end
  17. check
  18. end
  19.  
  20. def sqrt(n,d)
  21. #n is the square root value to the determined
  22. #d is the difference inputed
  23.  
  24. low = 0
  25. #low means the starting point of the iteration
  26. high = n
  27.  
  28. guess = (high + low) / 2.0
  29.  
  30. until (guess*guess) - n <= d && (guess*guess) -n >= 0 do
  31.  
  32. if (guess*guess) - n > d
  33.  
  34. high = guess
  35.  
  36. end
  37.  
  38. if (guess*guess) - n < 0
  39.  
  40. low = guess
  41.  
  42. end
  43.  
  44. guess = (low + high)/2
  45.  
  46. end
  47.  
  48. guess
  49.  
  50. end
  51.  
  52. value = prime(81,0.0000000003)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement