User:Spekkio: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 140: Line 140:


(defun fibcalc (n)
(defun fibcalc (n)
(let ((sqrtFive (sqrtn 5)))
(/ (- (powint (* 1/2 (+ 1 (sqrtn 5))) n) (powint (* 1/2 (- 1 (sqrtn 5))) n)) (sqrtn 5)))
(/ (- (powint (* 1/2 (+ 1 sqrtFive)) n) (powint (* 1/2 (- 1 sqrtFive)) n)) sqrtFive)))


(defun fibrec (n)
(defun fibrec (n)

Revision as of 12:31, 9 December 2011

My Favorite Languages
Language Proficiency
C Active
C++ Beginner
Java Active
JavaScript Active
SQL Active
Mathematica Active
MATLAB Active
UNIX Shell Beginner
Lisp Beginner
Erlang Beginner
POV-Ray Beginner
Pascal Rusty
x86 Assembly Rusty
VHDL Rusty


Hello! I'm a Electronic Engineer, working with electronics developement.


I love computer programming, and my hobby is to program small simple computer programs.

I usually get stuck on an idea that I think would be fun to try and solve.

I love learning new programming languages.

Found this page recently, and it is perfect for me. :D


Currently I'm working on Huffman coding in C, and implementing a multi precision library to calculate

maclaurin series for different functions sin/cos/sqrt/pow etc... (see my recent post in Talk:Nth root)

<lang lisp> (defun pie() 31415926535897932385/10000000000000000000)

(defun factorial (n)

 (if (= n 0)
     1
   (* n (factorial (- n 1)))))

(defun powint (a b)

 (if (= b 0)
     1
   (* a (powint a (- b 1)))))

(defun binom (alpha n)

 (if (= n 1)
     alpha
   (* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n))))

(defun power (a b n)

 (if (= n 0)
     1
   (+ (* (binom b n) (powint (- a 1) n)) (power a b (- n 1)))))

(defun powertest(a b n)

 (if
     (= (* (power a b (+ 10 n)) 1.0) (* (power a b n) 1.0))
     (power a b (+ 10 n))
   (powertest a b (+ 10 n))
     )

)

(defun pow (a b) (powertest a b 1))

(defun logS(x n) (if (= n 0) (* 2 x) (+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) (logS x (- n 1)) ) ))

(defun loge(x n) (logS (/ (- x 1) (+ 1 x)) n))

(defun expon(x n) (if (= n 0) 1 (+ (/ (powint x n) (factorial n)) (expon x (- n 1)))))

(defun squaren(x n) (expon (* 1/2 (loge x n)) n))

(defun squarentest(a n)

 (if
     (= (* (squaren a (+ 10 n)) 1.0) (* (squaren a n) 1.0))
     (squaren a (+ 10 n))
   (squarentest a (+ 10 n))
     )

)

(defun sqrtn (a) (squarentest a 1))

(defun powern(x a n) (expon (* a (loge x n)) n))

(defun powerntest(a b n)

 (if
     (= (* (powern a b (+ 10 n)) 1.0) (* (powern a b n) 1.0))
     (powern a b (+ 10 n))
   (powerntest a b (+ 10 n))
     )

)

(defun pown (a b) (powerntest a b 1))

(defun sine(x n)

 (if (= n 0)
     x
   (+ (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) (sine x (- n 1)))))

(defun sinetest(x n)

 (if
     (= (* (sine x (+ 10 n)) 1.0) (* (sine x n) 1.0))
     (sine x (+ 10 n))
   (sinetest x (+ 10 n))
     )

)

(defun mysin (x) (sinetest x 1))

(defun cosi(x n) (if (= n 0) 1 (+ (* (/ (powint -1 n) (factorial (* 2 n)) ) (powint x (* 2 n))) (cosi x (- n 1)))))

(defun tang(x n) (/ (sine x n) (cosi x n)))

(defun asine (x n) (if (= n 0) x (+ (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) (asine x (- n 1)))))


(defun asinetest(x n)

 (if
     (= (* (asine x (+ 10 n)) 1.0) (* (asine x n) 1.0))
     (asine x (+ 10 n))
   (asinetest x (+ 10 n))
     )

)

(defun myasin (x)

 (if (< x 1)
     (if (> x -1)

(asinetest x 1) (/ (pie) -2) )

   (/ (pie) 2)
   )

)

(defun fibcalc (n)

 (let ((sqrtFive (sqrtn 5)))
   (/ (- (powint (* 1/2 (+ 1 sqrtFive)) n) (powint (* 1/2 (- 1 sqrtFive)) n)) sqrtFive)))

(defun fibrec (n)

 (if (= n 0)
     0
   (if (= n 1)

1

     (+ (fibrec (- n 1)) (fibrec (- n 2))))))

</lang>

test:

[14]> (* (myasin 9/100) 1.0)
0.09012195
[14]> (sin 9/100)
0.08987855


Compare with Mathematica:

In[14]:= N[ArcSin[9/100], 8]
Out[14]= 0.090121945

(myasin) seems to be more accurate, though trying to compute (myasin 9999999/10000000) is very slow.

Test 2:

[4]> (* (myasin (mysin 1)) 1.0)
1.0
[4]> (asin (sin 1))
1.0