473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
28 18380
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,065 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,065 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,065 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,065 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
211368e
1 Bit
Expand|Select|Wrap|Line Numbers
  1. a=b=1n;while(1)[a,b]=[b,a+b]
Written in Javascript. However, this goes 1, 2, 3, 5, 8, and does not start with 0, 1, 1.
Jun 8 '22 #29

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

Similar topics

5
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
0
by: misho | last post by:
Write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is...
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
6
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
5
by: coleslaw01 | last post by:
Hello, I am trying to teach myself C++ while babysitting a stable network in Iraq and have put together a program to display the fibonacci sequence. It works with long and long double(output in...
1
by: iugamage | last post by:
Help me to write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For...
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.