Last active
August 29, 2015 14:12
-
-
Save mmstick/eadaec87fba7fc5d33eb to your computer and use it in GitHub Desktop.
A fibonacci generator in multiple languages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
// getSequences() returns the number of sequences to print. | |
int getSequences() | |
{ | |
int sequences; | |
write("Number of sequences: "); | |
readf("%d", &sequences); | |
return sequences; | |
} | |
// fibSequence types contain a list of fibonacci numbers. | |
struct fibSequence | |
{ | |
double sequence[] = [1]; | |
double last = 0; | |
double current = 1; | |
void appendSequence() | |
{ | |
const double temp = last; | |
last = current; | |
current += temp; | |
sequence ~= current; | |
} | |
} | |
// newFibSequence() returns a new sequence of fibonacci numbers. | |
fibSequence newFibSequence(int sequences) | |
{ | |
fibSequence fib; | |
if (sequences > 1) foreach (element; 1..sequences) fib.appendSequence(); | |
return fib; | |
} | |
void main() | |
{ | |
fibSequence fib = newFibSequence(getSequences()); | |
fib.sequence.writeln(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(macro_rules)] | |
use std::io::stdin; | |
macro_rules! readln | |
{ | |
() => { stdin().read_line().ok().expect("Failed to read line").as_slice().trim(); } | |
} | |
// Gets an integer from stdin indicating the number of fib sequences to print. | |
fn get_sequence_count() -> int | |
{ | |
print!("Number of sequences: "); | |
match from_str(readln!()) { | |
Some(x) => x, | |
None => 0i, | |
} | |
} | |
// fibSequence types contain a list of fibonacci numbers. | |
struct FibSequence | |
{ | |
number_sequence: Vec<int>, | |
last_num: int, | |
current_num: int, | |
} | |
impl FibSequence | |
{ | |
// Acts as a constructor to initialize the fibonacci sequence. | |
fn origin() -> FibSequence | |
{ | |
FibSequence{number_sequence: vec![1], last_num: 0, current_num: 1} | |
} | |
// Generate a new fibonacci sequence. | |
fn new(count: int) -> FibSequence | |
{ | |
let mut fib = FibSequence::origin(); | |
for _ in range(1i, count) { | |
fib.append_sequence(); | |
} | |
return fib; | |
} | |
// Appends the next number in the sequence. | |
fn append_sequence(&mut self) | |
{ | |
let temp = self.last_num; | |
self.last_num = self.current_num; | |
self.current_num += temp; | |
self.number_sequence.push(self.current_num); | |
} | |
} | |
fn main() | |
{ | |
let fib = FibSequence::new(get_sequence_count()); | |
println!("{}", fib.number_sequence) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A somewhat complex example of a fibonacci generator using structs. | |
package main | |
import "fmt" | |
// getNumSequences returns a number indicating the amount of numbers to get. | |
func getNumSequences() (num int) { | |
print("Number of sequences: ") | |
fmt.Scanf("%d", &num) | |
return | |
} | |
// fibSequence types contain a list of fibonacci numbers. | |
type fibSequence struct { | |
sequences []int | |
last int | |
current int | |
} | |
// appendSequence() appends the next fibonacci number to the list. | |
func (f *fibSequence) appendSequence() { | |
f.last, f.current = f.current, f.last+f.current | |
f.sequences = append(f.sequences, f.current) | |
} | |
// newFibSequence() returns a new sequence of fibonacci numbers. | |
func newFibSequence(sequences int) *fibSequence { | |
fibs := fibSequence{[]int{1}, 0, 1} | |
for index := 1; index != sequences; index++ { | |
fibs.appendSequence() | |
} | |
return &fibs | |
} | |
func main() { | |
fibs := newFibSequence(getNumSequences()) | |
fmt.Println(fibs.sequences) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a simple fibonacci generator using a function literal to generate | |
// fibonacci numbers instead of using a struct. | |
package main | |
import "fmt" | |
// getNumSequences() returns a number indicating the amount of numbers to get. | |
func getNumSequences() (num int) { | |
print("Number of sequences: ") | |
fmt.Scanf("%d", &num) | |
return | |
} | |
// fibonacciGenerator() returns a function literal for generationg a fib sequence. | |
func fibonacciGenerator() func() int { | |
last, current := 0, 1 | |
return func() int { | |
last, current = current, last+current | |
return current | |
} | |
} | |
// newFibonacciSequence() returns a list of fibonacci numbers. | |
func newFibSequence(sequences int) (sequence []int) { | |
appendFib := fibonacciGenerator() | |
sequence = append(sequence, 1) | |
for index := 1; index != sequences; index++ { | |
sequence = append(sequence, appendFib()) | |
} | |
return | |
} | |
func main() { | |
fibSequence := newFibSequence(getNumSequences()) | |
fmt.Println(fibSequence) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment