1. Miki Tebeka
  2. euler

Commits

Miki Tebeka  committed 336e7a7

linq

  • Participants
  • Parent commits f326b1f
  • Branches linq

Comments (0)

Files changed (1)

File csharp/e3.cs

View file
 Answer: 6857
 */
 using System;
-using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
 
 class e3 {
-    static IEnumerable longs() {
+    static IEnumerable<long> longs() {
         long i = 2;
         while (true) {
             yield return i++;
         }
     }
 
-    static IEnumerable filter(long p, IEnumerable nums) {
-        foreach (long i in nums) {
-            if ((i % p) != 0) {
-                yield return i;
-            }
-        }
-    }
-
-    static long head(IEnumerable items) {
-        foreach (long i in items) {
-            return i;
-        }
-
-        // We'll never get here, just make the compiler happy
-        return -1;
-    }
-
-    static IEnumerable primes() {
-        IEnumerable stream = longs();
+    static IEnumerable<long> primes() {
+        IEnumerable<long> stream = longs();
         while (true) {
-            long p = head(stream);
-            stream = filter(p, stream);
+            var p = stream.First();
+            stream = stream.Where(i => (i % p) != 0);
             yield return p;
         }
     }
     static void Main() {
         long n = 600851475143;
         long p = 0;
-        foreach (long i in primes()) {
+        foreach (var i in primes()) {
             if (i > n) {
                 break;
             }