Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

FIBONACCI FIB PRINT

Name: Anonymous 2009-12-04 20:18

FIBONACCI FIB PRRINT

Write a program that prints out a list of fibonacci numbers in a language of your choice.


/*        I'm using C, you LISPing cretins.         /
/  Remember to use your hot, stripping [coed] tags */

#include <stdio.h>

fibonacci (int fibs)
{
    int count, big = 0, small = 1;
    for (count = 0; count <= fibs; count++)
    {
        printf("\n#%2d - %10d", count, big);
        big = big + small;
        small = big - small;
    }
}

main()
{
    int x;
    printf("Enter the highest fib that you want to see: ");
    scanf("%i", &x);
    fibonacci (x);
}

Your move.

Name: Anonymous 2009-12-04 20:34

learn to write valid C if you're going to be an EXPERT C PROGRAMMER

Name: Anonymous 2009-12-04 20:46

>>1
Won't work once you reach 0x7FFFFFFF, which should be at fib(46) or so.

Here's a REAL and PROPER/NON-HACKY solution:

(defun memoize (fn)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (let ((cache (make-hash-table :test #'equal)))
    #'(lambda (&rest args)
        (multiple-value-bind (val win) (gethash args cache)
          (if win
              val
              (setf (gethash args cache)
                    (apply fn args)))))))

(defun fibs (n)
  (case n
    (0 0)
    (1 1)
    (2 1)
    (t (+ (fibs (- n 2)) (fibs (- n 1))))))

(setf (symbol-function 'fibs) (memoize #'fibs))

;; reads a number and prints 0-number fibs
(let ((*read-eval*) nil)
  (dotimes (i (read)) (print (fibs i))))

;;; Speed is decent, but memory usage is wasteful. A cutomized memoization function using arrays is possible, and here's a toy example of it:

(defun memoize-special (fn)
  (declare (optimize (speed 3) (safety 0) (debug 0))) 
  (let ((cache (make-array 1000 :adjustable t :initial-element nil)))
    #'(lambda (n)
        (let ((val (aref cache n)))
          (if val val
              (setf (aref cache n)
                    (funcall fn n)))))))


;;; Here's a zomgoptimized version for the ctards:
(defun print-fibs-to-n (n)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (do
   ((previous 1 current)
    (current 0 (+ previous current))
    (k 0 (1+ k)))
   ((= k n) current)   
    (print current)))

(let ((*read-eval*) nil)
  (print-fibs-to-n (read)))

Name: Anonymous 2009-12-04 20:48

Oh fuck, I keep forgetting the damn code tags:

(defun memoize (fn)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (let ((cache (make-hash-table :test #'equal)))
    #'(lambda (&rest args)
        (multiple-value-bind (val win) (gethash args cache)
          (if win
              val
              (setf (gethash args cache)
                    (apply fn args)))))))

(defun fibs (n)
  (case n
    (0 0)
    (1 1)
    (2 1)
    (t (+ (fibs (- n 2)) (fibs (- n 1))))))

(setf (symbol-function 'fibs) (memoize #'fibs))

;; reads a number and prints 0-number fibs
(let ((*read-eval*) nil)
  (dotimes (i (read)) (print (fibs i))))

;;; Speed is decent, but memory usage is wasteful. A cutomized memoization function using arrays is possible, and here's a toy example of it:

(defun memoize-special (fn)
  (declare (optimize (speed 3) (safety 0) (debug 0))) 
  (let ((cache (make-array 1000 :adjustable t :initial-element nil)))
    #'(lambda (n)
        (let ((val (aref cache n)))
          (if val val
              (setf (aref cache n)
                    (funcall fn n)))))))


;;; Here's a zomgoptimized version for the ctards:
(defun print-fibs-to-n (n)
  (declare (optimize (speed 3) (safety 0) (debug 0)))
  (do
   ((previous 1 current)
    (current 0 (+ previous current))
    (k 0 (1+ k)))
   ((= k n) current)   
    (print current)))

(let ((*read-eval*) nil)
  (print-fibs-to-n (read)))

Name: Anonymous 2009-12-04 21:04

>>2

I'm pretty sure I just compiled this shit at least 10 different times in the latest version of GCC. IHBT.

>>3-4

Yeah it's got integer wrap around, big whoop wanna fight about it?

Name: Anonymous 2009-12-04 21:05

>>5
Just because gcc lets you compile it doesn't mean it's valid, fucklip.

Name: Anonymous 2009-12-04 21:07

>>6
As in, you know, conforming to that funny little thing called The C Standard.  And gcc will compile code that is neither valid C89 nor C99 as long as you don't use --pedantic.

Name: Anonymous 2009-12-04 21:15

>>7
Enjoy your GNU C

Name: Anonymous 2009-12-04 21:15

def fib(n):
    a, b = 0, 1
    while 1:
        yield a
        a, b = b, a + b

a = fib()
for i in range(10):
    print a.next()

Name: Anonymous 2009-12-04 21:15

>>6-7

Just compiled it with the pedantic flag set, no errors. If there you'd like to point out an error in my code that the compiler missed, or if I do not conform to some standard, feel free to correct me, but otherwise

back to /b/, please.

Name: Anonymous 2009-12-04 21:18

>>9
my bad. remove the args


def fib():
    a, b = 0, 1
    while 1:
        yield a
        a, b = b, a + b

a = fib()
for i in range(10):
    print a.next()

Name: clever guy 2009-12-04 21:30

Lua:
for i = 1, 100 do print(math.floor(((1 + 5 ^.5) / 2)^i / (5^.5) + .5)) end

Name: Anonymous 2009-12-04 21:35

>>10
-c99 -pedantic -Wall -Werror or -ansi -pedantic -Wall -Werror are the only acceptable options. I didn't test your code, though.

Name: Anonymous 2009-12-04 21:49

>>13
warning: return type defaults to "int"

I guess you're right on that. To be fair, they use functions without return types all the time in K&R 2nd Edition, but I digress.


/*        I'm using C, you LISPing cretins.         /
/  Remember to use your hot, stripping [coed] tags */

#include <stdio.h>

void fibonacci (int fibs)
{
    int count, big = 0, small = 1;
    for (count = 0; count <= fibs; count++)
    {
        printf("\n#%2d - %10d", count, big);
        big = big + small;
        small = big - small;
    }
}

int main()
{
    int x;
    printf("Enter the highest fib that you want to see: ");
    scanf("%i", &x);
    fibonacci (x);
   
    return 0;
}
Better?

Name: Anonymous 2009-12-04 22:07


void print_fibs (int count) {
    int a = 1, b = 1;
    while (count--) {
        printf("%d\n",a);
        b += a;
        a = b - a;
    }
}

Name: Anonymous 2009-12-04 22:10

>>14
That book is over 20 years old. Things change. In this case it's definitely for the better.

Name: Anonymous 2009-12-04 22:36

LOL LISTS IN C LOL


import Data.List(foldl')
import Data.Bits(testBit,bitSize)
 
fib :: Int -> Integer
fib n = snd . foldl' fib' (1, 0) $ dropWhile not [testBit n k | k <- let s = bitSize n in [s-1,s-2..0]]

fib' :: (Num t) => (t,t) -> Bool -> (t, t)
fib' (f, g) p
 | p = (f*(f+2*g),f*f+g*g)
 | True = (f*f+g*g, g*(2*f-g))

Name: Anonymous 2009-12-04 23:42

>>17
At least the C code is readable.

Name: Anonymous 2009-12-05 2:44

Here's a bash snippet:

dd if=/dev/fibs bs=16b count=$COUNT

/dev/fibs is actually a named pipe to some wankery fibs implementation of your choice with the obvious constraints (one being that it is fixed output padded to 16 characters in case you missed that part.)

Name: Anonymous 2009-12-05 4:28

>>1
Gayest comment style ever.

Name: Anonymous 2009-12-05 4:33

>>16
Exactly. Just like math should finally accept that in some cases 99.9% == 100%.

Name: Anonymous 2009-12-05 4:59

>>21
First, they would need to accept the legitimacy of the == operator, and then IHBTBYA

Name: Anonymous 2009-12-05 5:51

I Have Been Tantalized By Young Anus

Name: Anonymous 2009-12-05 10:45

>>20

Are you implying that C style comments are gay?

Name: Anonymous 2009-12-05 10:53

>>24
No, this is gay:


/* I am a    /
/ big anus,  /
/ talk&feel */


Less gay:

  /* I am a    /
 / big anus,  /
/ talk&feel */


Not gay at all:


/* I am a
big anus,
talk&feel */


OR


/*
  I am a
  big anus,
  talk&feel
*/

Name: Anonymous 2009-12-05 11:15

import System

fibs = 0: scanl (+) 1 fibs

main = map print . fibs . head =<< getArgs

Name: Anonymous 2009-12-05 11:20

>>25

I'm actually kind of liking the "less gay" style.


  /* HAX  /
 /  MY   /
/ ANUS */

Name: Anonymous 2009-12-05 11:41

>>26
You mean print . (fibs !!) . head, surely.

Name: Anonymous 2009-12-05 12:39

>>25

/*********************
 * What about these? *
 *********************/

Name: Anonymous 2009-12-05 13:17

I think I might modify cpp to allow ``faggot commenting''

Name: Anonymous 2009-12-05 13:37

>>30

Please don't do ``that.''

Name: Anonymous 2009-12-05 13:40


/*
 *   I am a
 *   big anus
 *   talk&feel
 */

Name: Anonymous 2009-12-05 13:45

The /prog/-indentation style for comments;


              /*
       I am a
     big anus
    talk&feel
 */

Name: Anonymous 2009-12-05 14:11

Name: Anonymous 2009-12-05 14:23

>>28
OP said the program should print a list of fibonacci numbers, so no, I meant what I wrote.

Name: Anonymous 2009-12-05 15:09

>>28
On second thought, what I meant was mapM_ print . fibs . head .

Name: Anonymous 2009-12-05 16:06

>>36
But fibs is not a function, it is a list, so how can you compose it with other functions?

Perhaps mapM_ print . (`take` fibs) . read . head =<< getArgs then. Or mapM_ print . (`take` fibs) =<< readIO . head =<< getArgs.

Name: Anonymous 2009-12-05 16:26

>>37
Fuck, that's what I get for not testing code before posting it on /prog/. Yes, kind sir, you're absolutely right, what should have meant was mapM_ print . (`take` fibs) . read . head =<< getArgs. Thank you for pointing it out.

Name: Anonymous 2011-02-04 18:03

Name: Anonymous 2013-06-18 18:27

Name: Anonymous 2013-06-18 18:33

Name: Anonymous 2013-06-18 18:40

Name: Anonymous 2013-06-18 18:47

Name: Anonymous 2013-06-18 18:54

Name: Anonymous 2013-06-18 19:01

Name: Anonymous 2013-06-18 19:08


Don't change these.
Name: Email:
Entire Thread Thread List