diff --git a/.gitignore b/.gitignore index f0f19a6..0a016c7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ Zig/*/zig-out/ .idea/ .vscode/ result +*.wasm +a.out diff --git a/C++/fibonacci/Enarx.toml b/C++/fibonacci/Enarx.toml new file mode 100644 index 0000000..813dda2 --- /dev/null +++ b/C++/fibonacci/Enarx.toml @@ -0,0 +1,15 @@ +# Arguments +args = [ + "7", + "21" +] + +# Pre-opened file descriptors +[[files]] +kind = "stdin" + +[[files]] +kind = "stdout" + +[[files]] +kind = "stderr" diff --git a/C++/fibonacci/fibonacci.cpp b/C++/fibonacci/fibonacci.cpp index 07295a7..72c8d77 100644 --- a/C++/fibonacci/fibonacci.cpp +++ b/C++/fibonacci/fibonacci.cpp @@ -1,16 +1,34 @@ -// Simple Program to calculate Fibonacci Sequence of an integer input +#include #include +#include + using namespace std; -int FibonacciSequence(int num) { - if(num <= 1) { - return num ; - } - return FibonacciSequence(num-1) + FibonacciSequence(num-2); + +unsigned long fib(unsigned long i) { + if (i <= 1) { + return i; + } + return fib(i - 1) + fib(i - 2); } -int main(){ - cout << "Enter the Number" << endl; - int n ; - cin >> n ; - cout << "Fibonacci Sequence term at " << n << " " << "is " << FibonacciSequence(n) << endl; +int main(int argc, char *argv[]) { + if (argc <= 1) { + unsigned long n; + cout << "Enter a non-negative number:" << endl; + cin >> n; + cout << "Fibonacci sequence number at index " << n << " is " << fib(n) + << endl; + } else { + for (unsigned int i = 1; i < argc; i++) { + errno = 0; + unsigned long n = strtoul(argv[i], NULL, 10); + if (errno != 0) { + cerr << "Failed to parse argument into a number: " << strerror(errno) + << endl; + exit(1); + } + cout << "Fibonacci sequence number at index " << n << " is " << fib(n) + << endl; + } + } } diff --git a/C/fibonacci/Enarx.toml b/C/fibonacci/Enarx.toml new file mode 100644 index 0000000..813dda2 --- /dev/null +++ b/C/fibonacci/Enarx.toml @@ -0,0 +1,15 @@ +# Arguments +args = [ + "7", + "21" +] + +# Pre-opened file descriptors +[[files]] +kind = "stdin" + +[[files]] +kind = "stdout" + +[[files]] +kind = "stderr" diff --git a/C/fibonacci/fibonacci.c b/C/fibonacci/fibonacci.c index 18f96e1..97abf36 100644 --- a/C/fibonacci/fibonacci.c +++ b/C/fibonacci/fibonacci.c @@ -1,15 +1,34 @@ +#include #include +#include +#include -int FibonacciSequence(int num) { - if(num <= 1) { - return num ; - } - return FibonacciSequence(num-1) + FibonacciSequence(num-2); +unsigned long fib(unsigned long i) { + if (i <= 1) { + return i; + } + return fib(i - 1) + fib(i - 2); } -int main(){ - printf("Enter the Number\n"); - int n ; - scanf("%d",&n); - printf("Fibonacci Sequence term at %d is %d " , n , FibonacciSequence(n)); +int main(int argc, char *argv[]) { + if (argc <= 1) { + unsigned long n; + printf("Enter a non-negative number:\n"); + if (scanf("%lu", &n) != 1) { + fprintf(stderr, "Failed to read number from stdin\n"); + exit(1); + } + printf("Fibonacci sequence number at index %lu is %lu\n", n, fib(n)); + } else { + for (unsigned int i = 1; i < argc; i++) { + errno = 0; + unsigned long n = strtoul(argv[i], NULL, 10); + if (errno != 0) { + fprintf(stderr, "Failed to parse argument into a number: %s\n", + strerror(errno)); + exit(1); + } + printf("Fibonacci sequence number at index %lu is %lu\n", n, fib(n)); + } + } } diff --git a/Go/fibonacci/Enarx.toml b/Go/fibonacci/Enarx.toml new file mode 100644 index 0000000..813dda2 --- /dev/null +++ b/Go/fibonacci/Enarx.toml @@ -0,0 +1,15 @@ +# Arguments +args = [ + "7", + "21" +] + +# Pre-opened file descriptors +[[files]] +kind = "stdin" + +[[files]] +kind = "stdout" + +[[files]] +kind = "stderr" diff --git a/Go/fibonacci/go.mod b/Go/fibonacci/go.mod new file mode 100644 index 0000000..e510be7 --- /dev/null +++ b/Go/fibonacci/go.mod @@ -0,0 +1,3 @@ +module github.com/enarx/codex/Go/fibonacci + +go 1.17 diff --git a/Go/fibonacci/main.go b/Go/fibonacci/main.go index c9f2dfa..1751bd9 100644 --- a/Go/fibonacci/main.go +++ b/Go/fibonacci/main.go @@ -1,19 +1,45 @@ -// Simple Program to calculate fibonacci of input - package main -import "fmt" -func FibonacciRecursion(n int) int { - if n <= 1 { - return n - } - return FibonacciRecursion(n-1) + FibonacciRecursion(n-2) +import ( + "bufio" + "flag" + "fmt" + "log" + "os" + "strconv" +) + +func init() { + log.SetFlags(0) } -func main(){ - fmt.Print("Enter number : ") - var n int - fmt.Scanln(&n) +func fib(n uint64) uint64 { + if n <= 1 { + return n + } + return fib(n-1) + fib(n-2) +} + +func main() { + flag.Parse() + + args := flag.Args() + if len(args) == 0 { + fmt.Println("Enter a non-negative number:") + sc := bufio.NewScanner(os.Stdin) + sc.Scan() + b, err := sc.Bytes(), sc.Err() + if err != nil { + log.Fatal("Failed to read stdin: %s", err) + } + args = []string{string(b)} + } - fmt.Println("Fibonacci of", n , "is", FibonacciRecursion(n)); + for _, arg := range args { + n, err := strconv.ParseUint(arg, 10, 64) + if err != nil { + log.Fatalf("Failed to parse number: %s", err) + } + fmt.Printf("Fibonacci sequence number at index %d is %d\n", n, fib(n)) + } } diff --git a/Ruby/fibonacci/fibonacci.rb b/Ruby/fibonacci/fibonacci.rb index 150c416..1f76011 100644 --- a/Ruby/fibonacci/fibonacci.rb +++ b/Ruby/fibonacci/fibonacci.rb @@ -1,5 +1,15 @@ -def fibonacci( n ) +def FibonacciSequence( n ) return n if ( 0..1 ).include? n - ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) + ( FibonacciSequence( n - 1 ) + FibonacciSequence( n - 2 ) ) +end + +if ARGV.length > 0 + ARGV.each { |arg| + n = arg.to_i + puts "Fibonacci sequence number at index #{n} is #{FibonacciSequence(n)}" + } +else + puts "Which Fibonacci index to find? " + n = ARGF.gets.to_i + puts "Fibonacci sequence number at index #{n} is #{FibonacciSequence(n)}" end -puts fibonacci( 5 ) diff --git a/Zig/fibonacci/Enarx.toml b/Zig/fibonacci/Enarx.toml index 273b618..813dda2 100644 --- a/Zig/fibonacci/Enarx.toml +++ b/Zig/fibonacci/Enarx.toml @@ -6,7 +6,7 @@ args = [ # Pre-opened file descriptors [[files]] -kind = "null" +kind = "stdin" [[files]] kind = "stdout" diff --git a/Zig/fibonacci/src/main.zig b/Zig/fibonacci/src/main.zig index 38cb0c4..8f57848 100644 --- a/Zig/fibonacci/src/main.zig +++ b/Zig/fibonacci/src/main.zig @@ -29,7 +29,7 @@ pub fn main() !void { const stdin = std.io.getStdIn(); defer stdin.close(); - try out.print("No arguments specified, please specify Fibonacci sequence index: \n", .{}); + try out.print("Enter a non-negative number:\n", .{}); var buf: [19]u8 = undefined; if (try stdin.reader().readUntilDelimiterOrEof(&buf, '\n')) |arg| { try print_fibonacci(out, arg); diff --git a/flake.nix b/flake.nix index 0552e05..9a4eccc 100644 --- a/flake.nix +++ b/flake.nix @@ -71,7 +71,7 @@ fibonacci-c-wasm = final.pkgsCross.wasi32.runCommandCC "fibonacci" { pname = "fibonacci"; - version = "0.1.0"; + version = "0.2.0"; } '' mkdir -p "$out/bin" @@ -85,14 +85,13 @@ name = final.fibonacci-c-wasm.pname; wasm = "${final.fibonacci-c-wasm}/bin/fibonacci.wasm"; - # TODO: Read this from repo - conf = defaultConf final; + conf = "${self}/C/fibonacci/Enarx.toml"; }; fibonacci-cpp-wasm = final.pkgsCross.wasi32.runCommandCC "fibonacci" { pname = "fibonacci"; - version = "0.1.0"; + version = "0.2.0"; } '' mkdir -p "$out/bin" @@ -106,13 +105,12 @@ name = final.fibonacci-cpp-wasm.pname; wasm = "${final.fibonacci-cpp-wasm}/bin/fibonacci.wasm"; - # TODO: Read this from repo - conf = defaultConf final; + conf = "${self}/C++/fibonacci/Enarx.toml"; }; fibonacci-go-wasm = final.stdenv.mkDerivation rec { pname = "fibonacci"; - version = "0.1.0"; + version = "0.2.0"; src = "${self}/Go/fibonacci"; @@ -139,8 +137,7 @@ name = final.fibonacci-go-wasm.pname; wasm = "${final.fibonacci-go-wasm}/bin/fibonacci.wasm"; - # TODO: Read this from repo - conf = defaultConf final; + conf = "${self}/Go/fibonacci/Enarx.toml"; }; fibonacci-rust-wasm = naersk-lib.buildPackage {