//import java.util.Stack; import jdsl.simple.api.Stack; import jdsl.simple.ref.ArrayStack; import io.Stdio; public class fib { private int[] tab; private int XX; public static void main(String[] argv) { he(argv); } public static void he(String[] argv) { int x; try{ x= Integer.parseInt(argv[0]); } catch(NumberFormatException ee) { x=4;} if (x<1) x=4; fib q= new fib(x); } public fib(int x) { int r= fibr(x); int i= fibi(x); io.Stdio.promptln("Fib("+x+") is rec= "+r+", iter= "+i); } int fibr(int x) { if (x<=1) return 1; else return fibr(x-1)+fibr(x-2); } int fibi(int x) { String o; int n, a1, a2; Stack op = new ArrayStack(); Stack re = new ArrayStack(); Stack ar = new ArrayStack(); op.push("F"); ar.push( new Integer(x) ); while (!op.isEmpty()) { o= (String) op.pop(); if ( o.equals("F") ) { n= ( (Integer)ar.pop() ).intValue(); if (n==0 || n==1) re.push( new Integer(1) ); else { op.push("+"); op.push("F"); op.push("F"); ar.push( new Integer(n-1) ); ar.push( new Integer(n-2) ); } } else if ( o.equals("+") ) { a1= ( (Integer)re.pop() ).intValue(); a2= ( (Integer)re.pop() ).intValue(); re.push( new Integer(a1+a2) ); } } return ( (Integer)re.pop() ).intValue(); } private void skriv(int c) { if (c>0) { if (c<10) io.Stdio.prompt(" "); io.Stdio.prompt(c); io.Stdio.prompt(" : ["); } else { io.Stdio.prompt(" -["); } for (int k=0; k