468,514 Members | 1,647 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

CHALLENGE: Shortest Algorithim for Fibonacci Sequence on ONE line

The challenge is to create the shortest algorithim, In any programming language.

RULES:
1. All code must be on one line.
2. Languages that prevent this are disallowed. I.E Assembly, and of course COBOL. All other languages are fair game.
3. Sequence must go up to at least 4181.


If i missed a languge that should be disallowed please let me know...
My solution is in Java. weighing in @ 108 Characters w/o spaces.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class F{public static void main(String[] a){int f=0; int t=1;while(f<4181){System.out.println(f);t=f+(f=t);}}
  3.  
  4.  
---
Assembly - If there was a fbonacci contest for the least amout of byte code.. It would win every time....
Mar 10 '08 #1
27 8291
jkmyoung
2,057 Expert 2GB
should be able to do this, although it's bad style.
int f=0,t=1;
It's bad style not spacing your program out anyways.

c++ should be shorter:
editing...
Mar 12 '08 #2
jkmyoung
2,057 Expert 2GB
Can make it even shorter by making it a for loop instead:
int a=1,b=1;for(;b<4182;a=b+(b=a))System.out.println(b );

Also, < has to be < 4182 if you want 4181 to print for your algorithm.
Mar 12 '08 #3
MACKTEK
40
In C, the compiler will not let me use #include in the same line as any other.
So, not sure if this is "acceptable" per your rules.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
  3.  
If not, then
Expand|Select|Wrap|Line Numbers
  1. int main(){for(int i,j=1;i<2585;);i+=j;j+=i;}}
  2.  
will work, although you wont get any output, the series is there in the variables. And this should be ok since your rules did not specify output to the screen, just that a series was created.
Mar 13 '08 #4
That is correct. I should also have specified that Preprocessor directives are not language code, so they do not count twords the end character count. And thankyou for pointing out my mistakes. I will be the first to admit that i am a novice programmer....

So to answer you question...
Expand|Select|Wrap|Line Numbers
  1.  int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}} 
The code does count for a total of 58 characters. If my count is off please let me know.
Mar 13 '08 #5
Using the for loop, it is down to 85 characters. Still not as good as 58 though. Just goes to show you how uselessly verbose java is.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class f{public static void main(){int a=1,b=1;for(;b<4182;a=b+(b=a))System.out.println(b);
  3.  
Oh, and please post the character count. Will make this a bit easier and eliminate the guess work. Thank you.
Mar 13 '08 #6
You know i have too much time on my hands... My signature will now compile in GCC. I really need a life.. The next challenge will have to be: How to best abuse the Preprocessor...
Mar 16 '08 #7
improvcornartist
303 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. c=s=1;99.times{p c,s;c+=s;s+=c}
30 characters w/o whitespace
Mar 20 '08 #8
SpecialKay
109 100+
would it count if i just #defined a function, then called the function. Could get it down to 1 char.
if you are not counting the defines
Apr 2 '08 #9
Is the line one.I think the code is in number one but like coding this is the true code




Dim a as string
Dim b as integer
For Each b in a
Writeline.true=1
Apr 12 '08 #10
Hey this is python

Expand|Select|Wrap|Line Numbers
  1. def f():
  2.   r=0
  3.   p=1
  4.   for i in range(20) :
  5.     print r
  6.     r=r+p
  7.     p=r-p
  8. f()
there are around 48 characters w\o white space
Apr 17 '08 #11
Banfa
9,056 Expert Mod 8TB
So to answer you question...
Expand|Select|Wrap|Line Numbers
  1.  int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}} 
The code does count for a total of 58 characters. If my count is off please let me know.
  1. That is 68 characters
  2. That solution does not work, i needs to be initialised

Expand|Select|Wrap|Line Numbers
  1. int main(){for(int i=1,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
for 70 characters however it can be re-written in 69 characters like this
Expand|Select|Wrap|Line Numbers
  1. int main(){for(int i=1,j=1;i<4182;i+=j,j+=i){printf("%d %d ",i,j);}}
note these are all C++ solutions.

I'm likeing these solutions but they are not the shortest

Expand|Select|Wrap|Line Numbers
  1. void f(int a, int b){if(a>4182)return;printf("%d %d ",b,a);f(2*a+b,a+b);}int main(){f(1,1);}
  2.  
  3. int main(int a, char**p){int b=a&65535,c=a>>16;if(a!=1)printf("%d %d ",b,c);if(b<4181)main(a*2+b*65535+c,0);}
  4.  
  5.  
  6. int main(int a, char**p){int b=a==1?1:(int)p;printf("%d %d ",b,a);if(b<4181)main(a+a+b,(void*)(a+b));}
  7.  
at 95, 112 and 105 characters each.
Apr 23 '08 #12
Banfa
9,056 Expert Mod 8TB
Hey this is python

Expand|Select|Wrap|Line Numbers
  1. def f():
  2.   r=0
  3.   p=1
  4.   for i in range(20) :
  5.     print r
  6.     r=r+p
  7.     p=r-p
  8. f()
there are around 48 characters w\o white space
A. you can't do it in Python because you can put the code on 1 line unfortunately
B. Since in Python the spaces are part of the language syntax you should count the (makes the count 80), the code will not work without the spaces there are required. An analogy is count C or C++ code without the ; the code would not work without them they are a necessary part of the syntax. But this is just my opinion.
Apr 23 '08 #13
Banfa
9,056 Expert Mod 8TB
Here is a rework to 79 characters
Expand|Select|Wrap|Line Numbers
  1. f(a,b)int a,b;{printf("%d %d ",b,a);b<4181?f(2*a+b,a+b):0;}int main(){f(1,1);}
Apr 23 '08 #14
JosAH
11,448 Expert 8TB
In my own private language RPL (*) I can do it like this:

Expand|Select|Wrap|Line Numbers
  1. 0 1'{drop swap over+dup swriteln}18times
  2.  
If the results can be left on the data stack it shortens to this:

Expand|Select|Wrap|Line Numbers
  1. 0 1'{drop over over+}18times
  2.  
kind regards,

Jos

(*) RPL stands for Reverse Polish Lisp and at least one other person in this world
knows about it so there is a user community ;-)
May 12 '08 #15
AmberJain
884 Expert 512MB
Well, this is my solution in C------->

Expand|Select|Wrap|Line Numbers
  1. main(){for(int a=0,b=1,c;a<4182;c=a,a=b,b=b+c)printf("%d",a);}
COUNT=62 characters (including all characters in above code which are necessary to compile and run the program).

Of course, we will need to include header file stdio.h for this code to compile, but I think we are not counting that.

So this my solution with 62 characters in C.

What are your views about it?

==============
=AmbrNewlearner=
==============
May 30 '08 #16
pbmods
5,821 Expert 4TB
PHP won't win 'cause we gotta put a dollar sign in front of our variables ):
Jun 3 '08 #17
JosAH
11,448 Expert 8TB
I can't even program in my own language; this is the latest version which leaves
all the results on the stack:

Expand|Select|Wrap|Line Numbers
  1. 0 1'{2dupn+}18times
  2.  
nineteen characters (if I'm able to count ;-)

kind regards,

Jos
Jun 5 '08 #18
Banfa
9,056 Expert Mod 8TB
I can't even program in my own language; this is the latest version which leaves
all the results on the stack:

Expand|Select|Wrap|Line Numbers
  1. 0 1'{2dupn+}18times
  2.  
What is this language and what language have you implemented it in?
Jun 5 '08 #19
AmberJain
884 Expert 512MB
What is this language and what language have you implemented it in?
JosAH is probably talking about RPL. Check out REPLY #15 above (in the same thread) for more details.

With regards,
============
AmbrNewlearner
============
Jun 5 '08 #20
JosAH
11,448 Expert 8TB
What is this language and what language have you implemented it in?
I named it RPL (Reverse Polish Lisp). It's a combination of Forth and Lisp (that
seems to be a contradiction but it isn't). I've implemented it in quite a few languages
and the latest (and most mature) implementation uses Java version 1.6. One of
the first versions was in C (it even ran on small PDAs ;-)

If you're really interested drop me a PM and I'll send you the sources so you can
play with it too (I even have a tiny IDE for it and the language is still a bit under
construction).

Personally I like its symbolic expression rewriting rules; I consider them a basis
for a new CAS (Computer Algebra System). It can do quite amazing things already.

I'd be happy to get a bit of critique and ideas on the entire thing.

kind regards,

Jos
Jun 5 '08 #21
kadghar
1,295 Expert 1GB
this can be done even in Excel's VBA (note im bored in the office)

Expand|Select|Wrap|Line Numbers
  1. sub f():b=1:c=1:d=",":e="1,1":while c<4181:a=b:b=c:c=a+b:e=e & d & c:wend:msgbox e:end sub
using 91 characters, including white spaces (only the needed ones)

^.^

I'll see if i can optimize this
Jun 23 '08 #22
kadghar
1,295 Expert 1GB
The code before can be shorter (89 chars)

Expand|Select|Wrap|Line Numbers
  1. sub f():b=1:c=1:d=",":e="1,1":while c<4181:a=b:b=c:c=a+b:e=e &d &c:wend:msgbox e:end sub
Or you can use recurrence relation, in case you want another way to calculate it (isnt it beautiful?):

Expand|Select|Wrap|Line Numbers
  1. sub f():for i=1 to 19:a=a &"," &round(.4472*1.61803^i--.618^i):next:msgbox a:end sub
this one is 85 chars (including spaces).

Of course, VBA is not the best 'language' for this, but it works fine anyway.
Jun 23 '08 #23
I think AmbrNewlearner did great job!
Jul 3 '08 #24
nakiya
3
I dont have a lisp compiler atm :D

(defun i (a b c)(progn (print c)(i b c (+ a b))))(i 0 1 1)

pardon the errors, but i am pretty sure it can b shorter
Jun 1 '09 #25
in R
c(0,sapply(1:59,function(n){k=1:n;sum(choose(n-k,k-1))}))

57 characters
Oct 1 '10 #26
In Haskell:

Expand|Select|Wrap|Line Numbers
  1. fb=0:scanl (+) 1 fb
This returns the entire list of fibonacci numbers
May 25 '12 #27
@dkmiller100
Here it is in J language:

{:"1 +/\@|.^:(i.5000) 0 1
Jul 8 '12 #28

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Niks | last post: by
14 posts views Thread by felixnielsen | last post: by
reply views Thread by misho | last post: by
3 posts views Thread by veeru | last post: by
6 posts views Thread by Andrew Tatum | last post: by
1 post views Thread by iugamage | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.