Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: isPrime Boolean Method returns true for squares of primes

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default isPrime Boolean Method returns true for squares of primes

    	public boolean isPrime(int x){
    		if(x % 2 == 0 && x > 2)
    			return false;
    		if(x <= 1){
    			return false;
    		}
    		if(x == 2){
    			return true;
    		}
     
    		for(long i = 3; i < Math.sqrt(x); i+=2){
    			if(x % i == 0)
    				return false;
    		}
     
     
    		return true;

    If I pass through any integer that's a square of a prime number.. It returns with true. So...

    Input: 25
    Output: "Prime: True"

    Input: 49
    Output: "Prime: True"

    SOLVED: Sorry guys... I just solved it.. Within the For Loop, I never checked for the square root of "x".. So the odd squares would return true..
    Last edited by Staticity; September 29th, 2011 at 11:50 PM. Reason: Solved Issue
    Simplicity calls for Complexity. Think about it.


Similar Threads

  1. two dimensional matrix that returns back a boolean.
    By Bighairjersey in forum Java Theory & Questions
    Replies: 1
    Last Post: July 23rd, 2011, 03:01 PM
  2. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  3. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM