Anonymous avatar Anonymous committed 086ff49

solved euler/008 (go) and added data/vector (c)

Comments (0)

Files changed (4)

-#!/usr/bin/guile
-!#
+#!/usr/bin/guile !#
 
 (define (fib n)
 	(if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))
 
-(fib 30)
+(display (fib 30))
+#include <math.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "vector.h"
+
+#define vx(v) vref(v, 0)
+#define vy(v) vref(v, 1)
+#define vz(v) vref(v, 2)
+
+Vector *
+vmake(int len) {
+		Vector *new = malloc(sizeof(Vector));
+		new->len = len;
+		new->v = calloc(len + 1, sizeof(double));
+		return new;
+}
+
+int
+vlen(Vector *v) {
+	return v->len;
+}
+
+inline double
+vref(Vector *v, int k) {
+	return *(v->v + k);
+}
+
+inline void
+vset(Vector *v, int k, double val) {
+	*(v->v + k) = val;
+}
+
+Vector *
+v2(double x, double y) {
+	Vector *new = vmake(2);
+	vset(new, 0, x);
+	vset(new, 1, y);
+	return new;
+}
+
+Vector *
+v3(double x, double y, double z) {
+	Vector *new = vmake(3);
+	vset(new, 0, x);
+	vset(new, 1, y);
+	vset(new, 2, z);
+	return new;
+}
+
+
+void 
+vadd(Vector *dest, Vector *v1, Vector *v2) {
+	int len	= vlen(v1);
+	int count;
+	for(count = 0; count < len; count++)
+		vset(dest, count, vref(v1, count) + vref(v2, count));
+}
+
+void 
+vsub(Vector *dest, Vector *v1, Vector *v2) {
+	int len	= vlen(v1);
+	int count;
+	for(count = 0; count < len; count++)
+		vset(dest, count, vref(v1, count) - vref(v2, count));
+}
+
+void 
+vmul(Vector *dest, Vector *v1, Vector *v2) {
+	int len	= vlen(v1);
+	int count;
+	for(count = 0; count < len; count++)
+		vset(dest, count, vref(v1, count) * vref(v2, count));
+}
+
+void 
+vdiv(Vector *dest, Vector *v1, Vector *v2) {
+	int count;
+	int len	= vlen(v1);
+	for(count = 0; count < len; count++)
+		vset(dest, count, vref(v1, count) / vref(v2, count));
+}
+
+double
+vdist(Vector *va, Vector *vb) {
+	int len = vlen(va);
+	double acc = 0;
+	int count;
+	for(count = 0; count < len; count++)
+		acc = sqrt(pow(abs(vref(va,count) - vref(vb,count)), 2) + pow(acc, 2));
+	return acc;
+}
+
+double
+voff(Vector *v) {
+	int len = vlen(v);
+	double acc = 0;
+	int count;
+	for(count = 0; count < len; count++)
+		acc = sqrt(pow(vref(v,count), 2) + pow(acc, 2));
+	return acc;
+}
+
+void
+vscale(Vector *v, double m) {
+	int len = vlen(v);
+	int count;
+	double scale;
+
+	scale = m / voff(v);
+	for(count = 0; count < len; count++)
+		vset(v, count, vref(v,count) * scale);
+}
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#define vx(v) vref(v, 0)
+#define vy(v) vref(v, 1)
+#define vz(v) vref(v, 2)
+
+typedef struct Vector {
+		int len;
+		double *v;
+} Vector;
+
+Vector *vmake(int len);
+int vlen(Vector *v);
+double vref(Vector *v, int k);
+void vset(Vector *v, int k, double val);
+Vector *v2(double x, double y);
+Vector *v3(double x, double y, double z);
+
+void vadd(Vector *dest, Vector *v1, Vector *v2);
+void vsub(Vector *dest, Vector *v1, Vector *v2);
+void vmul(Vector *dest, Vector *v1, Vector *v2);
+void vdiv(Vector *dest, Vector *v1, Vector *v2);
+
+double vdist(Vector *v1, Vector *v2);
+double voff(Vector *v);
+
+void vscale(Vector *v, double m);
+
+#endif

euler/008-digitsum.go

 /* project euler problem 008
  * discover the largest product of five consecutive digits in a
- * 1000-digit number.
+ * 1000-digit number (see 008.dat)
  */
 
 package main
 
 import (
+	"container/list"
 	"fmt"
-	//"container/list"
 	"os"
 )
 
+/* read digits from a file into a channel */
 func readDigits(ch chan int, f *os.File) {
 	var buf [512]byte
 
 		case nr > 0:
 			for i := 0; i < nr; i++ {
 				var b byte = buf[i]
-				if('0' <= b && b <= '9') {
+				if '0' <= b && b <= '9' {
 					ch <- int(b - '0')
-				} else if (b != ' ' && b != '\r'&& b != '\n' && b != '\t') {
+				} else if b != ' ' && b != '\r' && b != '\n' && b != '\t' {
 					fmt.Fprintf(os.Stderr, "error: invalid input\n")
 					os.Exit(1)
 				}
 	}
 }
 
+/* read an integer stream from ch and find the greatest product
+ * of five consecutive digits numbers */
+func greatestProduct(ch chan int, n int) int {
+	l := list.New()
+	greatest := 0
+
+	/* read first set of consecutive digits */
+	for i := 0; i < n; i++ {
+		in := <- ch
+		if in < 0 {
+			fmt.Fprintf(os.Stderr,"error: too few digits of input\n")
+			os.Exit(1)
+		} else {
+			l.PushFront(in)
+		}
+	}
+
+	/* loop until EOF */
+	for {
+		prod := 1
+		for p := l.Front(); p != nil; p = p.Next() {
+			prod *= p.Value.(int)
+		}
+		if prod > greatest {
+			greatest = prod
+		}
+
+		in := <- ch
+		if in < 0 {
+			break
+		} else {
+			l.PushFront(in)
+			l.Remove(l.Back())
+		}
+	}
+
+	return greatest
+}
+
+/* hook up stdin to these two functions and run it */
 func main() {
 	ch := make(chan int)
 	go readDigits(ch, os.Stdin)
-	for i := 0; i < 1000; i++ {
-		num := <-ch
-		if num < 0 {
-			break
-		}
-		fmt.Println(num)
-	}
+	res := greatestProduct(ch, 5)
+	fmt.Printf("%v\n",res)
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.