Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Sieve of Eratosthenes

Name: Anonymous 2006-11-19 7:22

I'm trying to write it in Java but so far it doesn't even zero multiples of 2. Halp?

import java.util.*;
public class Sito {
    public static void main(String[] args) {
    int n=10;
    int[] zbior = new int[n];
    for(int i=0;i<zbior.length;i++)
    {
        zbior[i]=i+2;
    }

    for(int i=0;i<zbior.length;i++)
    {
        System.out.print(zbior[i]+"  ");
    }
    System.out.println();
   
    int liczbaSprawdzana=0;
    zbior[5]=0;
   
    for(int i=liczbaSprawdzana+1;i<zbior.length;i++)
    {
        if (zbior[i]==0) continue; /*&&zbior[i]%zbior[liczbaSprawdzana]==0*/
        if (zbior[i]%zbior[liczbaSprawdzana]==0)
            {
                zbior[i]=0;
            }
    }
   
   
    for(int i=0;i<zbior.length;i++)
    {
        System.out.print(zbior[i]+"  ");
    }
       
    }
}

Name: Anonymous 2006-11-19 8:37

Haskell owns.

sieve :: [Int] -> [Int]
sieve [] = []
sieve (x:xs) = x : sieve (filter (\ n -> (mod n x) /= 0) xs)


sieve [2..100]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

(Unfortunately, I didn't find out a way of avoiding the lambda expression through function composition, anyone know how to reverse args, or something, for the modulo?)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List