Skip to content

Instantly share code, notes, and snippets.

@triclops200
Last active December 23, 2015 13:49
Show Gist options
  • Save triclops200/6644302 to your computer and use it in GitHub Desktop.
Save triclops200/6644302 to your computer and use it in GitHub Desktop.
(defun nfib (a b c n)
(if (= n 0)
a
(nfib b c (/ (+ b c) a) (1- n))))
(defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
(defmacro with-gensyms (syms &body body)
`(let ,(mapcar #'(lambda (x) `(,x (gensym))) syms)
,@body))
(defmacro collect-list (end-condition next-value var)
"Generates a list based on the next-value provided."
(with-gensyms (acc)
`(let ((,acc nil))
(while (not ,end-condition)
(push ,var ,acc)
(setq ,var ,next-value))
(nreverse (cons ,var ,acc)))))
(defun range (x y &optional (step 1))
"Generates a range with an optional step parameter. It is non-inclusive."
(if (> (abs (- y (+ step x))) (abs (- y x)))
nil
(nbutlast (collect-list (= x y) (+ x step) x))))
(with-open-file (f "data.txt" :direction :output :if-exists :supersede)
(format f
"~{~a~%~}"
(loop for x in (range 1 2001) collect (nfib 2.0 1 2 x))))
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import SubplotZero
file = open("data.txt","rU")
xs = []
for line in file:
xs.append(float(line.strip()))
fig = plt.figure(1)
ax = SubplotZero(fig,111,
xlabel="N",
ylabel="Number of Prime Lengthed Chains Less Than or Equal To N Divided By N")
fig.add_subplot(ax)
x = range(1,len(xs)+1)
ax.plot(x,xs)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment