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

Pages: 1-

Homework number 4 - fibonacci sussquences.

Name: Anonymous 2010-01-19 4:00

Consider the generalized fibonacci sequences:
4, 1, 5, 6, 11, 17, 28, ...
3, 2, 5, 7, 12, 19, 31, ...
Write a program that takes two numbers as input and prints as many Sussmans as the tenth number in the resulting fibonacci sequence.
USE RECURSION.

Name: Anonymous 2010-01-19 4:32

gfibs a b = f where f@(_:t) = a:b:zipWith(+)f t
sussquence = ((($putStrLn"Sussman").replicateM_.(!!9)).).gfibs

Name: Anonymous 2010-01-19 4:36

So you give it two numbers and it makes a fibonacci 10 series deep?

You should be able to do that.

Name: Anonymous 2010-01-19 8:46

>>2
(!!9)
That's ugly, use head . tail . tail . tail . tail . tail . tail . tail . tail . tail instead.

Name: Anonymous 2010-01-19 9:29

heatttttttttd

Name: xiagox 2010-01-19 10:58

/* Consider the generalized fibonacci sequences:
 * 4, 1, 5, 6, 11, 17, 28, ...
 * 3, 2, 5, 7, 12, 19, 31, ...
 * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
 * Write a program that takes two numbers as input and prints as many Sussmans
 * as the tenth number in the resulting fibonacci sequence. */
#include <stdio.h>

#define RECUR 10
int fib(int m, int n, int *cntptr);

main(void){
  int i, j, k, sussnum, count=0;
  int *cntptr;
  cntptr = &count;

  printf("Enter two numbers as input.\nFirst:\n");
  scanf("%d", &i);
  printf("Second:\n");
  scanf("%d", &j);

  sussnum = fib(i, j, cntptr);
  for(k=0; k<sussnum; k++){
    printf("SUSSMAN ");
  }
  printf("%d sussmen :D\n", sussnum);
}

int fib(int m, int n, int *cntptr){
  int result = m + n;
  *(cntptr)+=1;
  if(*cntptr == RECUR-2)
    return result;
  return fib(n, result, cntptr); 
}
took me a while to get the pointer right

Name: Anonymous 2010-01-19 11:04

There is just not enough recursion going on here. This problem is begging for premature optimization and absurd generalization and failed generalization due to laziness!

#lang scheme

(define-syntax let-accessors
  (syntax-rules ()
    ((_ (m a b c d) exp ...)
     (let ((a (first (first m)))
           (b (second (first m)))
           (c (first (second m)))
           (d (second (second m))))
       exp ...))))
      
(define (maketrix a b c d)
  (list (list a b)
        (list c d)))

(define (m* m1 m2 . rest)
  (let-accessors (m1 a b c d)
    (let-accessors (m2 e f g h)
      (let ((result (maketrix (+ (* a e) (* b g))
                              (+ (* a f) (* b h))
                              (+ (* c e) (* d g))
                              (+ (* c f) (* d h)))))
        (if (empty? rest)
            result
           (apply m* result rest))))))

(define (m^ m n)
  (cond ((= 0 n) (maketrix 1 0 0 1))
        ((= 1 n) m)
        ((even? n) (let ((M (m^ m (/ n 2)))) ; omg optimized
                     (m* M M)))
        (else (m* m (m^ m (- n 1))))))

(define (fib n n_0 n_1)
  (cond ((zero? n) n_0)
        ((= 1 n) n_1)
        (else
         (let ((m (m^ (maketrix 1 1 1 0) (- n 1))))
           (let-accessors (m a b c d)
             (+ (* a n_1) (* b n_0)))))))

(define (homework-4 n_0 n_1)
  (let loop ((count (fib 9 n_0 n_1)))
    (unless (<= count 0)
      (display "Sussman")
      (newline)
      (loop (- count 1)))))

Name: Anonymous 2010-01-19 11:30

Really I should have generalized exponentiation a bit, just in case.
(define (exp b n *proc id)
  (cond ((< n 0) (error 'exp "Negative exponent" n))
        ((= 0 n) id)
        ((= 1 n) b)
        ((even? n) (let ((B (exp b (/ n 2) *proc id)))
                     (*proc B B)))
        (else (*proc b (exp b (- n 1) *proc id)))))

(define (m^ m n)
  (exp m n m* (maketrix 1 0 0 1)))

Name: Anonymous 2010-01-19 13:10


(defun custom-dynamic-fibs (a b &optional (n 9))
  (labels ((rec (a b k)
             (cond
               ((= n k) a)
               (t (rec b (+ a b) (1+ k))))))
    (case n
      (0 a)
      (1 b)
      (t (rec a b 0)))))

(defun print-sussmans (n &optional (stream *standard-output*))
  (loop repeat n do (format stream "~&SUSSMAN~&")))

(defun homework-4 (a b)
  (print-sussmans (custom-dynamic-fibs a b)))

Writing a memoized version, or a looping one (not allowed by the problem requirements) would have been more fun, but this is likely the most efficient, less memory consuming way.

Name: Anonymous 2010-01-19 14:29

import java.io.*;
public class PROGHomework
{
   public static void main(String[] args)
   {
      int a = 0;
      int b = 1;
      Sussquences.fiba(a,b,10);
   }

   static class Sussquences
   {
      // Parameter 'c' is the position of the Fibonacci element you want returned
      public static void fiba(int a, int b, int c)
      {
         if(c <= 0) { return; }
         else if(c == 1)
         {
            while(a > 0) { System.out.print("Sussman "); a--; }
         }
         else if(c-2 == 0)
         {
            while(b > 0) { System.out.print("Sussman "); b--; }
         }
         else { fiba(b, a+b, c-1); }
      }
   }
}

Name: Anonymous 2010-01-19 14:33

static
You are not a Java programmer.

Name: Anonymous 2010-01-19 14:38

>>10
For the record, I know I didn't need to make a nested class.  But I never seem to have a good reason to use nested classes in a serious program.

Name: Anonymous 2010-01-19 15:06

def Sussmanacci(a,b,position):
    if (position == 1):
        for x in range(0,a):
            print 'sussman'
    else:
        Sussmanacci(b,a + b,position-1)

Sussmanacci(input('Enter number 1:'),
            input('Enter number 2:'),
            input("What saucemans would you like?:"),);

yay

Name: Anonymous 2010-01-19 15:43

for i = 1, setmetatable({io.read()+0, io.read()+0}, {__index = function(z, n) return z[n-2] + z[n-1] end})[10] do print"Sussman" end

Name: Anonymous 2010-01-19 16:25

Fuck your recursion.

#include <stdio.h>

int main (void)
{
    int a, b, x;
    scanf("%d", &a), scanf("%d", &b);
   
    if (b > a)
        x = b, b = a, a = x;
   
    for (x = 2; x <= 10; x++)
        a += b, b = a - b;
   
    for (x = 1; x <= a; x++)
        printf("SUSSMAN NO. %d\n", x);
   
    return 0;
}


Can someone post their results for 5 and 6, just so I can be sure I did it right? I got 500.

Name: Anonymous 2010-01-19 17:04

>>15
Write two implementations of fib, so you can check your work1. Even so, there might be a question, so you'd be best to write four implementations of fib. Interestingly, your confidence in your results based on the agreement of n fib implementations grows on the order of O(n²)2.

1500 is correct.
2This is not true.

Name: Anonymous 2010-01-19 17:56

>>16
500 is correct
the tenth number in the resulting fibonacci sequence
tenth

Name: Anonymous 2010-01-19 18:23

>>13
print 'sussman\n' * 8

Name: Anonymous 2010-01-19 20:17

>>1
USE RECURSION.
CAN DO!

f=lambda f:(lambda x:f(lambda *args:x(x)(*args)))(lambda x:f(lambda *args:x(x)(*args)))
print f(lambda f:lambda x,y,n:n and f(y,x+y,n-1) or x*"sussman\n")(*([int(x) for x in __import__('sys').argv[1:]]+[9,]))


I love FIOC, it's so maintainable.

Name: Anonymous 2010-01-19 21:10

>>17
You are right, that is the eleventh. My bad.

Name: Anonymous 2010-01-19 21:14

>>23 is a fag.

Name: Anonymous 2010-01-19 21:18

>>17,20

So 309 is the correct answer?

#include <stdio.h>

int main (void)
{
    int a, b, x;
    scanf("%d", &a), scanf("%d", &b);
   
    if (b > a)
        x = b, b = a, a = x;
   
    for (x = 2; x < 10; x++)
        a += b, b = a - b;
   
    for (x = 1; x <= a; x++)
        printf("SUSSMAN NO. %d\n", x);
   
    return 0;
}

Name: Anonymous 2010-01-19 21:38

>>22
Lifted from SICP:
(define (general-fib b a p q n)
  (cond ((= n 0) b)
        ((even? n)
         (general-fib b
                      a
                      (+ (square p) (square q))
                      (+ (* 2 p q) (square q))
                      (/ n 2)))
        (else (general-fib (+ (* b p) (* a q))
                           (+ (* b q) (* a q) (* a p))
                           p
                           q
                           (dec n)))))
(map (lambda(x) (general-fib 5 6 0 1 x)) (build-list 10))

(5 6 11 17 28 45 73 118 191 309)

Also, fuck, I was >>21

Name: Anonymous 2010-01-20 9:34

I'm pretty sure unfoldr uses recursion.

import Control.Arrow
import Control.Monad
import List

sussfib = curry ((`replicateM_` putStr "sussman ") . (!!9) . unfoldr (Just . (fst &&& snd &&& uncurry (+))))

Name: Anonymous 2010-01-20 9:53

And so does iterate

import Control.Monad

sussfib = curry ((`replicateM_` putStr "sussman ") . fst . (!!9) . iterate (\(x, y) -> (y, x + y)))

Name: Anonymous 2010-01-20 10:23

>>11
Are you complimenting him?

Name: Anonymous 2010-01-20 11:16

>>23
Dear sweet christ, put that in an inner define.

Name: Anonymous 2010-01-20 12:55

>>27
Put who in the what now?

Name: Anonymous 2010-01-20 14:28

And so does fix.

import Control.Monad.Fix
import Control.Monad

sussfib x y = (`replicateM_` putStr "sussman ") . (!!9) . fix $ (x:) . scanl (+) y

Name: Anonymous 2010-01-20 17:00

import Control.Monad.Fix
import Control.Monad

sussfib x y = fix ((x:) . scanl (+) y) !! 9 `replicateM_` putStr "sussman "

Name: Anonymous 2010-01-21 16:44


(defun fib (n)
           (cond ((< n 2) 1)
                 ((print 'sussssman)
                  (+ (fib (- n 1))
                     (fib (- n 2))))))


(I have clearly made the program better by
      (printing the fibonacci number
           ((preceded by the number of whole predecessors the said number has) each of which is replaced by sussmen)))
(recursively of course).)

Name: Anonymous 2010-01-21 16:51

>>31
Way to read the assignment.

Name: HASKAL FAN 2010-01-21 17:25

>>32
Did you expect a Scheme programmer to make something useful? HA!

Name: Anonymous 2010-01-21 17:37

Scheme
Try again

Name: Anonymous 2010-01-22 13:56

>>34
Endoid Sexofunctor curried to HOT STRIPPING COEDS

Name: Anonymous 2010-01-22 16:01

>>35
StrictAnal

Name: Anonymous 2010-01-24 14:10

>>32
way to achieve satori

Name: Anonymous 2011-02-03 0:22

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