[Tutor] Making a Primary Number List generator

Marc Tompkins marc.tompkins at gmail.com
Tue May 14 02:13:51 CEST 2013


On Mon, May 13, 2013 at 4:01 PM, Daniel Magruder <dsm3 at att.net> wrote:

> Dear Dave,
> I am using python 2.
> I am still confused as what return does. What does it mean if a function
> returns True to the caller? What is the caller?
>

You've defined a function - isodd - but it doesn't automatically execute.
It has to be invoked - "called" - from the main process of your script.
Generically, "the caller" is the process that invoked the function.

When you call a function, you pass input to it - "arguments" or
"parameters" - and you (optionally) get back a result.  The way the
function delivers that result is via "return".  The function can "return"
whatever you want it to; this is Python, and you are in control.  True and
False (NOT the same as the strings "True" and "False", by the way!) are two
special values that we use for clarity; you could substitute ANY true
statement for True, and ANY false statement for False.
In fact, you could shorten your isodd() function to:
> def isodd(candidate):
>    return candidate%2 !=0:
and it would function identically.



> Your code worked for returning a list of 1000 items of odd numbers, so I
> then tried writing a code to replay isodd to give True or False for
> isprime. However,
> > def isodd(candidate):
> >    if candidate%2 ==0:
> >        return False
> >    else:
> >        return True
> The meaning of this I am still confused about. What does it mean if the
> value is returned as false if it has 0 remainder, what does it mean if it
> is returned as true?


If you divide a number by 2 and there's no remainder, it's even.  If there
IS a remainder, it's odd.
Since we're trying to see whether the number "is odd", we return False in
the first case, and True in the second.


Is this like a dictionary with keys and values?
> Also, here is my new code, which still does not work. I have no idea why
> not, and am getting very frustrated by the fact that making a program that
> determines if a number is prime or not is so hard for me. I would
> appreciate any assistance, and a verbal walk through of how to logically
> determine if a number is prime.
>

There are two truisms of education that I consider absolutely vital:
-  If you can't explain it simply, you don't fully understand it yourself
-  The best way to learn something is to try to teach someone else.

When you write a program, you are explaining the problem - in the simplest
possible terms - to a student who truly knows NOTHING of what you're
talking about.  So it is absolutely vital that you understand it
yourself... but at the same time, once you've finished your program you
will understand the problem better than before you started.

A prime number is one whose only integer factors are itself and 1.

Anyway, a very simplistic test for whether X is prime:
a -  2 is the first prime.  Add it to your list of primes.
b -  starting from 3, take a number Y and try to divide it by every item in
the list of primes.
   -  for each item in the list, if the remainder is 0 then Y is not prime
   -  if you reach the end of the list and haven't had a 0 remainder, add Y
to the list.
c -  if Y is equal to or greater than X, you've finished building your list.
d -  if X is in the list, it's prime.

As you can see, this method requires testing every number UP TO X* in order
to determine whether X is prime; your job is actually simpler - you can
stop at c).

* Actually, you only need to test numbers up to the square root of X, but
let's keep it simple for the moment.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130513/5219f865/attachment.html>


More information about the Tutor mailing list