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){
Related
I am trying to find palindrome no but every time it is showing false for every no even for 121
please Help....
public boolean isPalindrome(int x) {
if(x<0 || x%10==0){
return false;
}
int rev = 0;
while(x!=0){
rev=(rev*10)+(x%10);
x/=10;
}
if(x==rev){
return true;
}
else{
return false;
}
}
enter image description here
As an option, you may create something like this:
public boolean isPalindrome(int x) {
StringBuilder sb = new StringBuilder();
sb.append(x);
return sb.toString().equals(sb.reverse().toString());
}
Because after your while loop ends, x will be 0, you have to act on a copy instead
public boolean isPalindrome(int x) {
int num = x;
if(x<0 || x%10==0){
return false;
}
int rev = 0;
while(x!=0){
rev=(rev*10)+(x%10);
x/=10;
}
if(num==rev){
return true;
}
else{
return false;
}
}
All you need to do is build the new number as you reduce the original. Then compare the two.
for (int i : new int[]{121,12321, 123,34543,20012}) {
System.out.printf("%6d - %s%n", i, isPalindrome(i));
}
public static boolean isPalindrome(int numb) {
int n = 0;
for (int b = numb; b > 0;) {
n *= 10;
n += b%10;
b/=10;
}
return n == numb;
}
Prints
121 - true
12321 - true
123 - false
34543 - true
20012 - false
Hope this is useful:
public static boolean palindrome(int n) {
int nPalindrome = 0;
int nCopy = n;
while (n != 0) {
nPalindrome = nPalindrome *10 + n % 10;
n = n / 10;
}
if (nCopy == nPalindrom) {
return true;
} else {
return false;
}
}
You function can be as simple as below
public static void main(String args[]){
int r,sum=0;
int n=454;//It is the number variable to be checked for palindrome
if(isPalindrome(n)) {
System.out.println("palindrome number ");
} else {
System.out.println("not palindrome number ");
}
}
public boolean isPalindrome(int n) {
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
return n==sum;
}
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";
}
}
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);
}
}
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;
}
I'm trying to sort out some homework regarding "repetition" with the while statement. The homework is asking me to input a number and tell if said number is prime. So far, I've come with this:
class Prime {
boolean esPrime(int n) {
boolean prime = true;
int divisor = 2;
while (prime && divisor != n) {
if (n % divisor == 0) {
prime = false;
} else {
divisor++;
}
}
return prime;
}
}
Then I stated this "boolean test" in the main method to check if that piece of code worked:
boolean testEsPrime = esPrime(2) == false;
public static void main(String[] args) {
Prime p = new Prime();
System.out.println("testEsPrime = " + p.testEsPrime);
}
And whenever I run it I get false and I can't seem to be able to spot the error. Any clue why this happens?
It's not the most efficient code in the world, but it works for me (see below). I'm not sure what the problem is here! Incidentally, you only need to test 2 and odd factors up to the square root of the thing you're testing, and a more efficient approach in general (for determining what numbers are primes in bulk) is to use a Sieve of Erastothenes (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).
The reason you get false for the test you do is that esPrime(2) == false is false - that is, esPrime(2) == true.
class Prime {
boolean esPrime(int n) {
boolean prime = true;
int divisor = 2;
while (prime && divisor != n) {
if (n % divisor == 0) {
prime = false;
}
else {
divisor++;
}
}
return prime;
}
public static void main(String[] args) {
Prime p = new Prime();
for(int i=2; i<10; ++i) {
System.out.println("esPrime(" + i + ") = " + p.esPrime(i));
}
}
}
Output:
esPrime(2) = true
esPrime(3) = true
esPrime(4) = false
esPrime(5) = true
esPrime(6) = false
esPrime(7) = true
esPrime(8) = false
esPrime(9) = false