create a function which tells if a number is prime

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 835 Views - Last Post: 26 August 2014 - 06:45 AM Rate Topic: -----

#1 Letoatreides3508  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 21-July 14

create a function which tells if a number is prime

Posted 21 July 2014 - 05:38 PM

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
return True

test = 5

is_prime(test)


I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: create a function which tells if a number is prime

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:51 PM

** Renamed title to be more descriptive than 'help with code' **
Was This Post Helpful? 0
  • +
  • -

#3 Letoatreides3508  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 21-July 14

Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:53 PM

Ok... There doesn't seem to be any option to edit the title.. But thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Letoatreides3508  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 21-July 14

Re: create a function which tells if a number is prime

Posted 21 July 2014 - 05:59 PM

Also, not sure if this would be useful information or not, but the exact message reads 'assertion error'
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7965
  • View blog
  • Posts: 13,589
  • Joined: 19-March 11

Re: create a function which tells if a number is prime

Posted 21 July 2014 - 06:45 PM

View PostLetoatreides3508, on 21 July 2014 - 07:38 PM, said:

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
return True   <-----This line is outdented outside of the scope of the function declaration.
test = 5

is_prime(test)


I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?

Was This Post Helpful? 0
  • +
  • -

#6 f0ssil  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 22-July 14

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 09:11 AM

The error is because 'return True' is outside the function. Correct implementation would be

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
        else:
            return True

test = 5

is_prime(test)



Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,303
  • Joined: 11-December 07

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 09:32 AM

That's not right. How would the loop ever get to 3?
Was This Post Helpful? 2
  • +
  • -

#8 f0ssil  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 22-July 14

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 11:09 AM

View Postcfoley, on 22 July 2014 - 09:32 AM, said:

That's not right. How would the loop ever get to 3?



My bad, I was just correcting the indentation and not the logic. The logic should be :-

def is_prime(n):
    if n > 1:
        for i in range(2, n):
            if n % i == 0:
                return False
            else:
                return True
    else:
        return False



P.S - It does work for 3 as well.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7965
  • View blog
  • Posts: 13,589
  • Joined: 19-March 11

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 11:15 AM

This loop can never get past 2, because it always returns some value on the first item it sees, which is 2. So this is more of an is_even than an is_prime

@f0ssil: There are a few other issues about this function that should be addressed, as well
I have a blog post about finding primes that you might find useful.
Was This Post Helpful? 1
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,308
  • Joined: 21-June 11

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 01:48 PM

View Postf0ssil, on 22 July 2014 - 08:09 PM, said:

My bad, I was just correcting the indentation and not the logic.


The original logic was fine (for inputs greater than 1 anyway). You broke it by moving the return true into an else in the loop rather than having it after the loop.
Was This Post Helpful? 1
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,303
  • Joined: 11-December 07

Re: create a function which tells if a number is prime

Posted 22 July 2014 - 02:20 PM

Quote

P.S - It does work for 3 as well.


Aaaah well that is interesting. Your second version gives the correct answer for 1--8. It looks as if all is well but then you ask if 9 is prime and it says "yes".

Jon.kiparsky and sepp2k have explained it well.
Was This Post Helpful? 0
  • +
  • -

#12 pythonuser007  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 26
  • Joined: 25-January 14

Re: create a function which tells if a number is prime

Posted 22 August 2014 - 12:59 AM

View PostLetoatreides3508, on 21 July 2014 - 05:38 PM, said:

I'm trying to get my homework done for my python programming class, and the assignment is to create a function which tells me if a number is prime. This is the code I entered:

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
return True

test = 5

is_prime(test)


I'm getting an indentation error message, but I can't figure out what isn't being indented correctly. Can someone please tell me where I'm going wrong here?



I think the problem is that your not using braces.

#include <stdio.h>

int check_prime(int prime);

main()
{
 int result, prime;
 printf("\nEnter a number you want to check for it being a prime number: ");
 scanf("%d", &prime);
 puts("");
 result = check_prime(prime);
 if (result == 0)
   printf("\nYour number is not a prime number.\n");
 else
   printf("\nYour number is a prime number.\n");
}

int check_prime(int prime)
{
 int loop;
 for (loop = 2; loop < prime; loop++)
 {
  if ((prime % loop) == 0)
  {
   return 0;
  }
 }
 return 1;
}


Was This Post Helpful? -1
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7965
  • View blog
  • Posts: 13,589
  • Joined: 19-March 11

Re: create a function which tells if a number is prime

Posted 22 August 2014 - 06:56 AM

View Postpythonuser007, on 22 August 2014 - 02:59 AM, said:

I think the problem is that your not using braces.



You're aware that we're talking about python, right?
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: create a function which tells if a number is prime

Posted 22 August 2014 - 06:59 AM

Python is a snake. Snake starts with a C.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#15 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-November 08

Re: create a function which tells if a number is prime

Posted 26 August 2014 - 06:31 AM

def is_prime(num):
	if num % 2 == 0:
		return False
	else:
		return True

number = 9

if is_prime(number):
	print("%i is prime" % number)
else:
	print('%i is even' % number)

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2