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. -
-
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);}}
-
-
---
Assembly - If there was a fbonacci contest for the least amout of byte code.. It would win every time....
28 18380
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...
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.
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. - #include<stdio.h>
-
int main(){for(int i,j=1;i<2585;){printf("%d %d ",i,j);i+=j;j+=i;}}
-
If not, then -
int main(){for(int i,j=1;i<2585;);i+=j;j+=i;}}
-
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.
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... - 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.
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. -
-
class f{public static void main(){int a=1,b=1;for(;b<4182;a=b+(b=a))System.out.println(b);
-
Oh, and please post the character count. Will make this a bit easier and eliminate the guess work. Thank you.
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...
- c=s=1;99.times{p c,s;c+=s;s+=c}
30 characters w/o whitespace
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
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
Hey this is python - def f():
-
r=0
-
p=1
-
for i in range(20) :
-
print r
-
r=r+p
-
p=r-p
-
f()
there are around 48 characters w\o white space
Banfa 9,065
Expert Mod 8TB
So to answer you question... - 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.
- That is 68 characters
- That solution does not work, i needs to be initialised
- 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 - 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 -
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);}
-
-
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);}
-
-
-
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));}
-
at 95, 112 and 105 characters each.
Banfa 9,065
Expert Mod 8TB
Hey this is python - def f():
-
r=0
-
p=1
-
for i in range(20) :
-
print r
-
r=r+p
-
p=r-p
-
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.
Banfa 9,065
Expert Mod 8TB
Here is a rework to 79 characters - 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);}
In my own private language RPL (*) I can do it like this: -
0 1'{drop swap over+dup swriteln}18times
-
If the results can be left on the data stack it shortens to this: -
0 1'{drop over over+}18times
-
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 ;-)
Well, this is my solution in C-------> - 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=
==============
PHP won't win 'cause we gotta put a dollar sign in front of our variables ):
I can't even program in my own language; this is the latest version which leaves
all the results on the stack:
nineteen characters (if I'm able to count ;-)
kind regards,
Jos
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:
What is this language and what language have you implemented it in?
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
============
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
this can be done even in Excel's VBA (note im bored in the office) - 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
The code before can be shorter (89 chars) - 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?): - 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.
I think AmbrNewlearner did great job!
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
in R
c(0,sapply(1:59,function(n){k=1:n;sum(choose(n-k,k-1))}))
57 characters
In Haskell:
This returns the entire list of fibonacci numbers
@dkmiller100
Here it is in J language:
{:"1 +/\@|.^:(i.5000) 0 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Niks |
last post by:
Can anybody explain me what is a "Fibonacci search"?
even an URL will do.
Thanks for reading.
|
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...
|
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;...
|
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...
|
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
|
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:
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |