Created
March 20, 2012 01:53
-
-
Save lgaff/2129908 to your computer and use it in GitHub Desktop.
Common lisp recursion vs. Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; The idea here is to be able to determine velocity and distance | |
;;; attained as a function of time. | |
;; some physical constants/definitions. | |
(defconstant +g+ 9.81) | |
(defconstant +C+ 2.998e8) | |
;; Helper to determine if we've hit C (we'll use 99.9% as the cutoff) | |
(defun velocity-cap (velocity accel) | |
(if (<= (+ velocity accel) (* +C+ 0.999)) | |
(+ velocity accel) | |
+C+)) | |
;; How fast are we travelling if we accelerate at 1g for n seconds. | |
(defun current-velocity (velocity time) | |
(if (= time 0) | |
velocity | |
(current-velocity (velocity-cap velocity +g+) (- time 1)))) | |
;; Same, but for distance from earth after time seconds | |
(defun current-displacement (velocity time distance) | |
(if (= time 0) | |
distance | |
(current-displacement (velocity-cap velocity +g+) (- time 1) (+ distance (+ velocity +g+))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
G = 9.81 | |
C = 2.998e8 | |
def velocity_cap (velocity,accel) | |
if (velocity + accel) <= (C * 0.999) then | |
return velocity + accel | |
else | |
return C | |
end | |
end | |
def current_velocity (velocity,time) | |
while (time > 0) do | |
velocity = velocity_cap(velocity, G) | |
time -= 1 | |
end | |
return velocity | |
end | |
def current_displacement (velocity, time, distance) | |
while time > 0 do | |
distance += velocity | |
velocity = velocity_cap(velocity, G) | |
time -= 1 | |
end | |
return distance | |
end | |
many_days = 25000 * 24 * 60 * 60 | |
puts "Starting run" | |
puts Time::now | |
start_time = Time::now | |
puts "Velocity attained after #{many_days} seconds (" + many_days./(24)./(60)./(60).to_s + " days)" | |
puts current_velocity(0,many_days).round.to_s + " Meters per second" | |
end_time = Time::now - start_time | |
puts "Velocity computed in #{end_time} seconds" | |
puts "Distance travelled" | |
start_time = Time::now | |
puts current_displacement(0, many_days, 0).round./(1000).to_s + " Kilometers" | |
end_time = Time::now - start_time | |
puts "Distance computed in #{end_time} seconds" | |
puts "End of run" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment