uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

Prime.java (716B)


      1 import java.io.*;
      2 
      3 class Prime {
      4 	public static void main(String[] args) {
      5 		try {
      6 			FileOutputStream fos = new FileOutputStream(args[0]);
      7 			BufferedOutputStream bos = new BufferedOutputStream(fos);
      8 			DataOutputStream dos = new DataOutputStream(bos);
      9 			int i = 0;
     10 			int n = 2;
     11 
     12 			while (i < 100) {
     13 				if (is_prime(n)) {
     14 					dos.writeInt(n);
     15 					i++;
     16 				}
     17 				n++;
     18 			}
     19 			dos.close();
     20 			fos.close();
     21 		} catch (FileNotFoundException e) {
     22 			System.err.println("File not found");
     23 			return;
     24 		} catch (IOException e) {
     25 			System.err.println("IO Exception");
     26 			return;
     27 		}
     28 	}
     29 
     30 	private static boolean is_prime(int n) {
     31 		for (int i = 2; i < n; i++)
     32 			if (n % i == 0)
     33 				return false;
     34 		return true;
     35 	}
     36 }