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:
Anonymous2010-01-19 4:32
gfibs a b = f where f@(_:t) = a:b:zipWith(+)f t
sussquence = ((($putStrLn"Sussman").replicateM_.(!!9)).).gfibs
Name:
Anonymous2010-01-19 4:36
So you give it two numbers and it makes a fibonacci 10 series deep?
/* 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);
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:
Anonymous2010-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!
(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)))))))
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)))))
(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:
Anonymous2010-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:
Anonymous2010-01-19 14:33
static
You are not a Java programmer.
Name:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-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.
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,]))
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:
Anonymous2010-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)
sussfib x y = fix ((x:) . scanl (+) y) !! 9 `replicateM_` putStr "sussman "
Name:
Anonymous2010-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).)