View previous topic :: View next topic |
Author |
Message |
Some Guy Expert Cheater
Reputation: 1
Joined: 29 Aug 2009 Posts: 100 Location: In front of my computer, duh!
|
Posted: Wed May 26, 2010 7:26 pm Post subject: Could someone please write this for me? |
|
|
Hello,
I am rather newbie at programming. Could someone please write me a Fibonacci sequence calculator? While any one would work, my request is one which you enter the number of numbers to calculate, then it calculates it and exports it to a text document.
Thanks a lot!
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed May 26, 2010 7:28 pm Post subject: |
|
|
Code: | #include <stdio.h>
#define STEPS 40
unsigned int fib(int n)
{
if (n == 0) { return 0; }
if (n == 1) { return 1; }
return fib(n - 1) + fib(n - 2);
}
unsigned int fib2(int n)
{
static unsigned int f[STEPS] = { 0, 1, 1 };
for(int i = 2 ; i <= n ; i++) //f0 = 0, f1 = 1. 0, 1, 1, 3. just skip if not past 1 yet.
{
if(f[i] == 0) //has it been calculated yet? if it isn't 0, then yes, it has.
f[i] = (fib2(n - 1) + (fib2(n - 2 )));
}
return f[n];
}
int main()
{
for(int i = 0; i < STEPS; i++)
printf("%u\n", fib2(i)); //fast
for(int i = 0; i < STEPS; i++)
printf("%u\n", fib(i)); //slow
return 0;
} |
|
|
Back to top |
|
 |
Some Guy Expert Cheater
Reputation: 1
Joined: 29 Aug 2009 Posts: 100 Location: In front of my computer, duh!
|
Posted: Wed May 26, 2010 7:30 pm Post subject: |
|
|
slovach wrote: | Code: | #include <stdio.h>
#define STEPS 40
unsigned int fib(int n)
{
if (n == 0) { return 0; }
if (n == 1) { return 1; }
return fib(n - 1) + fib(n - 2);
}
unsigned int fib2(int n)
{
static unsigned int f[STEPS] = { 0, 1, 1 };
for(int i = 2 ; i <= n ; i++) //f0 = 0, f1 = 1. 0, 1, 1, 3. just skip if not past 1 yet.
{
if(f[i] == 0) //has it been calculated yet? if it isn't 0, then yes, it has.
f[i] = (fib2(n - 1) + (fib2(n - 2 )));
}
return f[n];
}
int main()
{
for(int i = 0; i < STEPS; i++)
printf("%u\n", fib2(i)); //fast
for(int i = 0; i < STEPS; i++)
printf("%u\n", fib(i)); //slow
return 0;
} |
|
Thanks. Any chance of a binary form of that?
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed May 26, 2010 7:57 pm Post subject: |
|
|
heh, check the bottom for the link
Code: | 001000110110100101101110011000110110110001110101011001000110010100100000001111000111001101110100011001000110100101101111001011100110100000111110000
011010000101000001101000010100010001101100100011001010110011001101001011011100110010100100000010100110101010001000101010100000101001100100000001101
000011000000001101000010100111010101101110011100110110100101100111011011100110010101100100001000000110100101101110011101000010000001100110011010010
110001000101000011010010110111001110100001000000110111000101001000011010000101001111011000011010000101000100000001000000010000001101001011001100010
000000101000011011100010000000111101001111010010000000110000001010010010000001111011001000000111001001100101011101000111010101110010011011100010000
000110000001110110010000001111101000011010000101000100000001000000010000001101001011001100010000000101000011011100010000000111101001111010010000000
110001001010010010000001111011001000000111001001100101011101000111010101110010011011100010000000110001001110110010000001111101000011010000101000001
101000010100010000000100000001000000111001001100101011101000111010101110010011011100010000001100110011010010110001000101000011011100010000000101101
001000000011000100101001001000000010101100100000011001100110100101100010001010000110111000100000001011010010000000110010001010010011101100001101000
010100111110100001101000010100000110100001010011101010110111001110011011010010110011101101110011001010110010000100000011010010110111001110100001000
000110011001101001011000100011001000101000011010010110111001110100001000000110111000101001000011010000101001111011000011010000101000100000001000000
010000001110011011101000110000101110100011010010110001100100000011101010110111001110011011010010110011101101110011001010110010000100000011010010110
111001110100001000000110011001011011010100110101010001000101010100000101001101011101001000000011110100100000011110110010000000110000001011000010000
000110001001011000010000000110001001000000111110100111011000011010000101000001101000010100010000000100000001000000110011001101111011100100010100001
101001011011100111010000100000011010010010000000111101001000000011001000100000001110110010000001101001001000000011110000111101001000000110111000100
000001110110010000001101001001010110010101100101001001000000010111100101111011001100011000000100000001111010010000000110000001011000010000001100110
001100010010000000111101001000000011000100101110001000000011000000101100001000000011000100101100001000000011000100101100001000000011001100101110001
000000110101001110101011100110111010000100000011100110110101101101001011100000010000001101001011001100010000001101110011011110111010000100000011100
000110000101110011011101000010000000110001001000000111100101100101011101000010111000001101000010100010000000100000001000000111101100001101000010100
010000000100000001000000010000000100000001000000110100101100110001010000110011001011011011010010101110100100000001111010011110100100000001100000010
100100100000001000000010000000101111001011110110100001100001011100110010000001101001011101000010000001100010011001010110010101101110001000000110001
101100001011011000110001101110101011011000110000101110100011001010110010000100000011110010110010101110100001111110010000001101001011001100010000001
101001011101000010000001101001011100110110111000100111011101000010000000110000001011000010000001110100011010000110010101101110001000000111100101100
101011100110010110000100000011010010111010000100000011010000110000101110011001011100000110100001010001000000010000000100000001000000010000000100000
001000000010000000100000011001100101101101101001010111010010000000111101001000000010100001100110011010010110001000110010001010000110111000100000001
011010010000000110001001010010010000000101011001000000010100001100110011010010110001000110010001010000110111000100000001011010010000000110010001000
000010100100101001001010010011101100001101000010100010000000100000001000000111110100001101000010100000110100001010001000000010000000100000011100100
110010101110100011101010111001001101110001000000110011001011011011011100101110100111011000011010000101001111101000011010000101000001101000010100110
100101101110011101000010000001101101011000010110100101101110001010000010100100001101000010100111101100001101000010100010000000100000001000000110011
001101111011100100010100001101001011011100111010000100000011010010010000000111101001000000011000000111011001000000110100100100000001111000010000001
010011010101000100010101010000010100110011101100100000011010010010101100101011001010010000110100001010001000000010000000100000001000000010000000100
000011100000111001001101001011011100111010001100110001010000010001000100101011101010101110001101110001000100010110000100000011001100110100101100010
001100100010100001101001001010010010100100111011001000000010000000100000001011110010111101100110011000010111001101110100000011010000101000001101000
010100010000000100000001000000110011001101111011100100010100001101001011011100111010000100000011010010010000000111101001000000011000000111011001000
000110100100100000001111000010000001010011010101000100010101010000010100110011101100100000011010010010101100101011001010010000110100001010001000000
010000000100000001000000010000000100000011100000111001001101001011011100111010001100110001010000010001000100101011101010101110001101110001000100010
110000100000011001100110100101100010001010000110100100101001001010010011101100100000001000000010000000100000001000000010000000101111001011110111001
101101100011011110111011100001101000010100000110100001010001000000010000000100000011100100110010101110100011101010111001001101110001000000011000000
111011000011010000101001111101 |
http://www.mediafire.com/?yhhhzdimm0n
|
|
Back to top |
|
 |
Some Guy Expert Cheater
Reputation: 1
Joined: 29 Aug 2009 Posts: 100 Location: In front of my computer, duh!
|
Posted: Wed May 26, 2010 9:29 pm Post subject: |
|
|
Problem: I open fib.exe, some numbers quickly go up the screen, then the program closes after 2.5 seconds or so without any input or doing anything particularly useful.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed May 26, 2010 10:10 pm Post subject: |
|
|
run it from the command line.
it goes up to the 90th number.
|
|
Back to top |
|
 |
AtheistCrusader Grandmaster Cheater
Reputation: 6
Joined: 23 Sep 2006 Posts: 681
|
Posted: Thu May 27, 2010 1:40 am Post subject: |
|
|
Or you could just add a right before the return 0, in main.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu May 27, 2010 5:42 am Post subject: |
|
|
or no because that's a horrible solution
|
|
Back to top |
|
 |
freddan007 Grandmaster Cheater Supreme
Reputation: 6
Joined: 04 Nov 2007 Posts: 1156 Location: Sweden
|
Posted: Thu May 27, 2010 9:15 am Post subject: |
|
|
slovach wrote: | or no because that's a horrible solution | getch();?
_________________
60+, 3-Hitler |
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu May 27, 2010 10:02 pm Post subject: |
|
|
Look ma, I'm learnin' F#!
Code: | open System.IO
open System.Numerics
let Fibonacci = Seq.unfold( fun (f, s) -> Some(f, (s, f + s)) ) (BigInteger 0, BigInteger 1)
let FibTo n = [for x in Fibonacci |> Seq.take n -> x]
let Limit = 9001 // Set this to anything... you can go pretty high.
let FibToLimit = FibTo (Limit+1)
File.WriteAllLines("fib.txt", [for x in 0..Limit do yield sprintf "%A -> %A" x FibToLimit.[x]]) |
|
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Wed Jun 02, 2010 11:03 pm Post subject: |
|
|
I got a better one...allows unlimited range of numbers. Not just 90.
http://www.mediafire.com/?rzqnzynzm4u
Im still not sure if it works on other peoples comp, due to me changing compilers, so test and tell me.
If it doesn't, ive prepared a file with 100,000 numbers.
http://www.mediafire.com/?m41mundwjr2
SOURCE: Code: | #include <NTL/ZZ.h>
#include <fstream>
NTL_CLIENT
int main()
{
system("cls");
ZZ count;
ZZ first;
ZZ second;
ZZ third;
ofstream myfile;
myfile.open("Fib_Sequencce.rtf");
system("title Fibonacci");
system("color 4");
cout << "Please insert how many iterations you want: ";
cin >> count;
first=0;
second=1;
third=0;
system("cls");
cout << "Working...Please do not close the window.";
myfile << first << "\n" << second << "\n";
while (count >0)
{
third=first + second;
first=second;
second=third;
myfile << fixed << third << "\n";
count=count-1;
}
system("cls");
cout << "Finished!, please check the folder for your file.\n\n";
cin.get();
return 0;
}
|
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jun 03, 2010 2:15 am Post subject: |
|
|
If we're turning this into an ebushido contest, then allow me:
Code: | open System
open System.IO
open System.Numerics
let FibonacciTo (n : BigInteger) =
((BigInteger.Zero, BigInteger.Zero), (BigInteger.One, BigInteger.One))
|> Seq.unfold (fun (f, s) -> Some (f, (s, (fst s + BigInteger.One, snd f + snd s))))
|> Seq.takeWhile (fun (a, _) -> a <= n)
let rec Main() =
Console.Write "How many numbers do you wish to calculate? (0 to exit) "
let (Success, Limit) = Console.ReadLine() |> BigInteger.TryParse
match Success with
| true when Limit > BigInteger.Zero ->
File.WriteAllLines ("fib.txt", [for (a, b) in FibonacciTo Limit do yield sprintf "%A -> %A" a b])
Console.WriteLine "Done!"
| true when Limit = BigInteger.Zero ->
Console.WriteLine "Bye!"
| _ ->
Console.WriteLine "Invalid input!"
Main()
Console.Title <- "I am 12 and what is this?"
Main() |
|
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Thu Jun 03, 2010 2:53 am Post subject: |
|
|
Flyte wrote: | If we're turning this into an ebushido contest, then allow me:
Code: | open System
open System.IO
open System.Numerics
let FibonacciTo (n : BigInteger) =
((BigInteger.Zero, BigInteger.Zero), (BigInteger.One, BigInteger.One))
|> Seq.unfold (fun (f, s) -> Some (f, (s, (fst s + BigInteger.One, snd f + snd s))))
|> Seq.takeWhile (fun (a, _) -> a <= n)
let rec Main() =
Console.Write "How many numbers do you wish to calculate? (0 to exit) "
let (Success, Limit) = Console.ReadLine() |> BigInteger.TryParse
match Success with
| true when Limit > BigInteger.Zero ->
File.WriteAllLines ("fib.txt", [for (a, b) in FibonacciTo Limit do yield sprintf "%A -> %A" a b])
Console.WriteLine "Done!"
| true when Limit = BigInteger.Zero ->
Console.WriteLine "Bye!"
| _ ->
Console.WriteLine "Invalid input!"
Main()
Console.Title <- "I am 12 and what is this?"
Main() |
|
whoa, what language is that?
also, i really didn't mean to sound like i was starting a contest. I figured i would include my program as well, because it can go over 90. Sorry to come off as rude.
|
|
Back to top |
|
 |
Some Guy Expert Cheater
Reputation: 1
Joined: 29 Aug 2009 Posts: 100 Location: In front of my computer, duh!
|
Posted: Thu Jun 03, 2010 9:35 am Post subject: |
|
|
Evil Intentions wrote: | I got a better one...allows unlimited range of numbers. Not just 90.
http://www.mediafire.com/?rzqnzynzm4u
Im still not sure if it works on other peoples comp, due to me changing compilers, so test and tell me.
If it doesn't, ive prepared a file with 100,000 numbers.
http://www.mediafire.com/?m41mundwjr2
SOURCE: Code: | #include <NTL/ZZ.h>
#include <fstream>
NTL_CLIENT
int main()
{
system("cls");
ZZ count;
ZZ first;
ZZ second;
ZZ third;
ofstream myfile;
myfile.open("Fib_Sequencce.rtf");
system("title Fibonacci");
system("color 4");
cout << "Please insert how many iterations you want: ";
cin >> count;
first=0;
second=1;
third=0;
system("cls");
cout << "Working...Please do not close the window.";
myfile << first << "\n" << second << "\n";
while (count >0)
{
third=first + second;
first=second;
second=third;
myfile << fixed << third << "\n";
count=count-1;
}
system("cls");
cout << "Finished!, please check the folder for your file.\n\n";
cin.get();
return 0;
}
|
|
Really liking this one.
|
|
Back to top |
|
 |
Evil_Intentions Expert Cheater
Reputation: 65
Joined: 07 Jan 2010 Posts: 214
|
Posted: Thu Jun 03, 2010 4:54 pm Post subject: |
|
|
Some Guy wrote: |
Really liking this one.  |
is the .exe working on your comp? Im not sure if my compiler settings are right. The first time i compiled it, i used visual c++, but it didn't work on any other comp but mine. So this one uses gcc, or g++, whatever it is, and it worked on my other computer as well as this one.
|
|
Back to top |
|
 |
|