aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m2/primes
diff options
context:
space:
mode:
Diffstat (limited to 'sem5/oop/m2/primes')
-rw-r--r--sem5/oop/m2/primes/.classpath6
-rw-r--r--sem5/oop/m2/primes/.gitignore1
-rw-r--r--sem5/oop/m2/primes/.project17
-rw-r--r--sem5/oop/m2/primes/src/Main.java32
-rw-r--r--sem5/oop/m2/primes/src/PrimeGen.java40
5 files changed, 96 insertions, 0 deletions
diff --git a/sem5/oop/m2/primes/.classpath b/sem5/oop/m2/primes/.classpath
new file mode 100644
index 0000000..fb50116
--- /dev/null
+++ b/sem5/oop/m2/primes/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/sem5/oop/m2/primes/.gitignore b/sem5/oop/m2/primes/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/sem5/oop/m2/primes/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/sem5/oop/m2/primes/.project b/sem5/oop/m2/primes/.project
new file mode 100644
index 0000000..b2abcc1
--- /dev/null
+++ b/sem5/oop/m2/primes/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>primes</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/sem5/oop/m2/primes/src/Main.java b/sem5/oop/m2/primes/src/Main.java
new file mode 100644
index 0000000..e191835
--- /dev/null
+++ b/sem5/oop/m2/primes/src/Main.java
@@ -0,0 +1,32 @@
+
+public class Main {
+
+ public static void main(String[] args) {
+ System.out.println("Lets find some primes!!");
+
+ int runto = 10000;
+ if (args.length > 0) {
+ runto = Integer.parseInt(args[0]);
+ }
+
+ // Calculate length of runto as string
+ int numlen = String.valueOf(runto).length();
+
+ PrimeGen pg = new PrimeGen();
+
+ int p;
+ int index = 1;
+ while ((p = pg.next(runto)) != -1) {
+ // Terrible two level format
+ System.out.printf(String.format("%%-%dd ", numlen), p);
+ if (index % 10 == 0) {
+ System.out.print(System.lineSeparator());
+ }
+ index++;
+ }
+ System.out.print(System.lineSeparator());
+ System.out.printf("Found %d primes%n", index-1);
+
+ }
+
+}
diff --git a/sem5/oop/m2/primes/src/PrimeGen.java b/sem5/oop/m2/primes/src/PrimeGen.java
new file mode 100644
index 0000000..9c91645
--- /dev/null
+++ b/sem5/oop/m2/primes/src/PrimeGen.java
@@ -0,0 +1,40 @@
+import java.io.PrintStream;
+import java.util.ArrayList;
+
+public class PrimeGen {
+
+ private ArrayList<Integer> primes;
+ private int at;
+
+ public PrimeGen() {
+ this.primes = new ArrayList<>();
+
+ this.at = 1;
+ }
+
+ public int next(int limit) {
+ // Run until a prime
+ primeloop:
+ while (true) {
+ this.at++;
+ if (this.at >= limit) {
+ return -1;
+ }
+
+ for (int prime : this.primes) {
+ if (this.at % prime == 0) {
+ continue primeloop;
+ }
+ }
+ break;
+ }
+
+ this.primes.add(at);
+ return at;
+ }
+
+ public int next() {
+ return next(-1);
+ }
+
+}