int n = (new IntReader()).readInt(); import java.io.*; // a class to read in an
ID: 3600414 • Letter: I
Question
int n = (new IntReader()).readInt(); import java.io.*; // a class to read in an integer public class IntReader{ private LineNumberReader d; public IntReader(){ d = new LineNumberReader(new BufferedReader(new InputStreamReader(System.in))); } // end of constructor public int readInt(){ try{ return(Integer.parseInt(d.readLine())); }catch(Exception e){} return(Integer.MAX_VALUE); } // end of readInt } // end of class IntReader int n = (new IntReader()).readInt(); import java.io.*; // a class to read in an integer public class IntReader{ private LineNumberReader d; public IntReader(){ d = new LineNumberReader(new BufferedReader(new InputStreamReader(System.in))); } // end of constructor public int readInt(){ try{ return(Integer.parseInt(d.readLine())); }catch(Exception e){} return(Integer.MAX_VALUE); } // end of readInt } // end of class IntReader
readNumber update entry/n -reado exit /notPrime true [n != 01/ i )notPrime = false handleZero checkPrime doDivide exit / notPrime = true [i > notPrime) writeResult [notPrime writeYes entry/ write(prime") InotPri writeNo entry/writenot prime")
Explanation / Answer
Hey!
I have given the solution, hope you are looking for this one only.
/** From here the code begines. I wrote modules as in state diagram ***/
import java.io.*;
public class Primeno {
public static void main(String[] args) {
IntReader ob = new IntReader();
int n = ob.readInt();
BusinessLogic obj = new BusinessLogic();
if(obj.handleZero(n)) {
obj.write();
}
else {
obj.checkPrime(n, 2);
obj.write();
}
}
}
class BusinessLogic {
boolean notPrime = false;
public boolean handleZero(int n) {
if (n == 0) {
this.notPrime = true;
return true;
}
return false;
}
public void checkPrime(int n, int i) {
if (i <= n/2 && !notPrime) {
doDevide(i, n);
}
}
public void doDevide(int i, int n) {
if (n % i == 0) {
i = update(i);
}
else {
checkPrime(n, i+1);
}
}
public int update(int i) {
this.notPrime = true;
return ++i;
}
public void write() {
if (notPrime) {
System.out.println("Not Prime");
}
else {
System.out.println("Prime");
}
}
}
// a class to read in an integer
class IntReader {
private LineNumberReader d;
public IntReader() {
d = new LineNumberReader(new BufferedReader(new InputStreamReader(System.in)));
} // end of constructor
public int readInt() {
try {
return (Integer.parseInt(d.readLine()));
} catch (Exception e) {
}
return (Integer.MAX_VALUE);
} // end of readInt
} // end of class IntReader
Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.