static boolean isPrime(int num){
int consNum = num; //something like having a non-changing value
if(consNum < 2){
return false;
}
else if( consNum % Math.round(num--/2) == 0 && num > 2)
return false;
}
else{
if(num==1)
return true;
else
return isPrime(num);
}
}
I'm trying to create a function that will determine if num is a prime number. Problem, i want a value(consNum) to stay with that value during the first call, Is there a way to do this recursively?
Edit
from:
if( (consNum % (int)(num--/2) + 0.5 == 0 )
to:
if( (consNum % Math.round(num--/2) == 0 && num > 2)
Local variables are local to the invocation of the particular method;
recursive methods are no exception.
If you wish to pass that value down the invocation chain, you need to make a second parameter for it, and pass it down explicitly:
// Users of your code invoke this method
public static boolean isPrime(int num) {
return isPrime(num, num);
}
// This overload with two parameters is the actual recursive method
private static boolean isPrime(int num, int original) {
if(original%(int)((num--/2)+0.5)==0)
return false;
}
else{
if(num==1)
return true;
else
return isPrime(num, original);
}
}
Try this
static boolean isFirst=true;
static int consNum;
static boolean isPrime(int num){
if(isFirst){ // This condition will help you to keep consNum with the initial value
consNum=num;
isFirst=false;
}
if(consNum%(int)((num--/2)+0.5)==0)
return false;
}
else{
if(num==1)
return true;
else
return isPrime(num);
}
}
Related
I'm trying to find out if a number is a prime number or not. I created this method, which I will be using in another class later.
When compiling it tells me that I need a return statement outside the for-loop, but if I try to return the boolean value it gives me an error (cannot find symbol). What shall I return?
public class NumeroPrimo {
public static boolean primo(int numero){
for (int i=2; i<numero/2; i++){
if(numero%i==0){
return false;
}
else return true;
}
}
}
If the loop was not done (numero 1), no return would happen.
Also you return true too often.
public static boolean primo(int numero) {
for (int i = 2; i <= numero/2; i++) {
if (numero % i == 0){
return false;
}
}
return true;
}
Also for 4 <= is required.
I am posting here two functions. In the findPrime(int m, int i, int n) I have one outer if and outer else block from both the block I am getting return so it can be considered that the function is returning something I put same structure in endOther(String a, String b) i.e.
there are two main if-else blocks both are returning value as per function's return type so we can say that function is returning something but the endOther(String a, String b)
function is throwing compile time error saying that put return statement while first function is not throwing such error. I am not understanding this issue please help me. Type both the functions in eclipse and check
1.
static boolean findPrime(int m, int i, int n){
if(n == 0 || n == 1)
return false;
else {
if(i <= m) {
if(n%i == 0)
//if(m%i == 0)
return false;
else {
i++;
//return findPrime(m,i);
return findPrime(m,i,n);
}
}
else {
return true;
}
}
}
2.
public boolean endOther(String a, String b) {
if(a.length()==b.length()) {
if(a.equals(b))
return true;
else
return false;
}
else {
if(a.length()>b.length()) {
if(a.substring(a.length()-b.length(),b.length()).equals(b))
return true;
}
else {
if(b.substring(b.length()-a.length(),a.length()).equals(a))
return true;
else
return false;
}
}
}
Your endOther function must return a value on all possible execution paths. In case a.length() > b.length() the return may not be executed based on the condition of the inner if.
public boolean endOther(String a, String b) {
if(a.length()==b.length()) {
if(a.equals(b))
return true;
else
return false;
}
else {
if(a.length()>b.length()) {
// IF THIS FAILS THERE IS NO RETURN!
if(a.substring(a.length()-b.length(),b.length()).equals(b))
return true;
}
else {
if(b.substring(b.length()-a.length(),a.length()).equals(a))
return true;
else
return false;
}
}
}
As a general note, you could use some of the methods in String to improve your code, e.g. String.endsWith instead of the substring operation. This would be more readable. a and b being identical is a special case of String.endsWith, so the following should be equivalent:
public boolean endOther(String a, String b) {
return a.endsWith(b) || b.endsWith(a);
}
My homework is to Design a class named MyInteger with the following conditions:
An int data field named value that stores the int value of an integer.
A constructor that creates a MyInteger object for the specified int value.
A get method that returns the int value.
A method, isPrime() that returns true if the value is a prime number. See -section 4.10 of the text for Java code that detects prime numbers (this may vary depending on the edition you have).\
A static, isPrime(MyInteger) that returns true if the value is a prime number. Note that this method takes an object reference variable (rather than the value) as a parameter.
My problems come up in the static boolean isPrime method, stating that "/" and "%" are undefined for arguement type and in the main method in my if statement: isPrime() == true. It says to change it to static, but i already have a static boolean isPrime method and I'm supposed to have two isPrime methods according to my conditions. Thank you if you are able to help.
public class MyInteger {
public MyInteger(int value){
}
public static int getValue(){
int value = 997;
return value;
}
public boolean isPrime(){
int value = 997;
for (int i=2; i<=value/2; i++){
if(value % i == 0) {
return false;
}
}
return true;
}
public static boolean isPrime(MyInteger value){
for(int i=2; i<=value/2; i++){
if(value%i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
MyInteger value = new MyInteger(MyInteger.getValue());
if (isPrime()==true && isPrime(value)==true){
System.out.println("Testiwng Instance method, is Prime");
System.out.println("isPrime: " + value + " is prime");
System.out.println("--------------------------------");
System.out.println("Testing Class method (That takes a reference variable) is Prime");
System.out.println("isPrime: " + value + " is prime");
}
else{
System.out.println("Testiwng Instance method, is Prime");
System.out.println("isPrime: " + value + " is not prime");
System.out.println("--------------------------------");
System.out.println("Testing Class method (That takes a reference variable) is Prime");
System.out.println("isPrime: " + value + " is not prime");
}
}
}
You don't have to go till the half of the number to check whether it is prime. You can have a loop that checks only the numbers from 2 to the square root of your number. See this - StackOverflow question about checking prime numbers
I believe you need something like this:
public class Main {
public static void main(String[] args) throws IOException {
Scanner inp = new Scanner(System.in);
int someValue = inp.nextInt();
MyInteger myInt = new MyInteger(someValue);
System.out.println("Testing instance method:");
System.out.println(myInt.isPrime());
System.out.println("Testing static method:");
System.out.println(MyInteger.isPrime(myInt));
}
}
class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isPrime() {
int sqrt = (int) Math.sqrt((double)value);
for(int i = 2; i <= sqrt; i++) {
if (value % i == 0) return false;
}
return true;
}
public static boolean isPrime(MyInteger myInt) {
return myInt.isPrime();
}
}
Here's a good reference for testing prime numbers What would be the fastest method to test for primality in Java?
Primarily, change your isPrime() to
boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
Your value variable in the method you mentioned is of the type MyInteger, but you're trying to use it as an int. you probably want to use value.getValue() instead.
You should consider using class level variable to make use of constructor to initialize it.
Also in main() method you are trying to access non-static method (isPrime()), use it as value.isPrime().
In case you don't want to use class variable, make use of static getValue() method inside your static boolean isPrime(MyInteger value) method which solves your problem
Another way to check the prime number
public String isPrime(int number) {
if (number < 0) {
return "not a valid number";
}
if (number == 0 || number == 1) {
return "not a prime number";
}
if (number == 2 || number == 3) {
return "prime number";
}
if ((number * number - 1) % 24 == 0) {
return "prime number";
} else {
return "not a prime number";
}
}
I have to do the method, that I give a number in, if it is a Square number, it will get me true back
if it's not it will get me false back,
I wrote the code:
public class Aufgabe {
static boolean x;
public static boolean istQuadratzahl(int zahl){
int n = (int) Math.sqrt(zahl);
if (zahl%n == 0){
x = true;
}
else {
return x=false;
}
return x;
}
public static void main(String []args){
System.out.println(istQuadratzahl(6));
}
}
but when I give 6 or 8 in, it gives me true back, where did I go wonge here?
In your case, sqrt(6) is 2.44948974278. When you cast it to int it becomes 2. Of course, 6 % 2 = 0.
Try to check the result with:
if (zahl == n * n){
x = true;
}
This will do the trick:
public boolean isSquare(double zahl){
double m=Math.sqrt(zahl);
double n=(int)Math.sqrt(zahl);
if(m==n)
return true;
else
return false;
}
int sqrt = (int) Math.sqrt(inputNumber);
if(sqrt*sqrt == number) {
System.out.println(number+" is a perfect square number!");
return true;
}else {
System.out.println(number+" is NOT a perfect square number!");
return false;
}
Don't use a static variable. In fact, don't use a variable at all.
In addition, your logic is wrong. (int)Math.sqrt(12) == 3, 12%3 == 0, but 12 is not a square.
public static boolean istQuadratzahl(int zahl)
{
int n = (int) Math.sqrt(zahl);
if (n*n == zahl)
return true;
else
return false;
}
change the 'if' to this
if (zahl == n * n){
I have written a program to find whether a number is prime or not.
The program has 2 methods:
take input from user as to the numbers(stored in an array)
take each element of the array and to find whether it is prime or not (this method return type is boolean)
Now my 2nd method is always returning true for all values.
public static boolean Isprime(int x){
boolean isprime = false;
for(int m=2;m<x/2;m++){
int temp = x%m;
if(temp == 0){
isprime = false;
}
else{
isprime = true;
}
}
return isprime;
}
Edited:
public static boolean Isprime(int x){
boolean isprime = false;
for(int m=2;m<=x/2;m++){
int temp = x%m;
if(temp == 0){
isprime = false;
break;
}
else{
isprime = true;
}
}
return isprime;
}
P.S - It is working for 9 as well.
You need to break out of for loop as soon as you found if it is not a prime and slighlty modified approach you can follow to omit some code and optimize it as well.
public static boolean Isprime(int x){
boolean isprime = true;
for(int m=2;m<=Math.sqrt(x);m++){
int temp = x%m;
if(temp == 0){
isprime = false;
break;
}
}
return isprime;
}