Problems on these two questions

Discussion in 'Python' started by su29090, Nov 19, 2012.

  1. su29090

    su29090 Guest

    I all of the other problems but I have issues with these:

    1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

    2.An arithmetic progression is a sequence of numbers in which the distance(or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.

    Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 .

    Thanks in advance.
    su29090, Nov 19, 2012
    #1
    1. Advertising

  2. On 19/11/2012 01:52, su29090 wrote:
    >
    > I all of the other problems but I have issues with these:
    >
    > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
    >
    > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.
    >
    > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 .
    >
    > Thanks in advance.
    >


    Please specify what programming language and OS you're using, what code
    you've tried so far, and what problems if any you're having. If you're
    using Python please give us the version, if not please try another
    mailing list.

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Nov 19, 2012
    #2
    1. Advertising

  3. su29090

    su29090 Guest

    On Sunday, November 18, 2012 8:52:35 PM UTC-5, su29090 wrote:
    > I did all of the other problems but I have issues with these:
    >
    >
    >
    > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
    >
    >
    >
    > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6,12, 18, 24, ... , the distance is 6.
    >
    >
    >
    > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 .
    >
    >
    >
    > Thanks in advance.


    I'm using Python 3.2
    su29090, Nov 19, 2012
    #3
  4. su29090

    Dave Angel Guest

    On 11/18/2012 08:52 PM, su29090 wrote:
    > I all of the other problems but I have issues with these:
    >
    > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)


    if is_a_prime(n):
    is_prime = True

    Now all you have to do is write is_a_prime(). if you get stuck, please
    show us what you've got, and what the problem is with it. And as usual,
    tell us what version of Python you're writing in, if any.

    > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.
    >
    > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 .


    Don't call it 'sum' since that's a built-in function. Coincidentally,
    it's a function that takes an iterable, and calculates the sum of its
    elements. Sounds useful, no? The other thing you might want is xrange,
    which takes a start value, and end value, and a distance value.



    >
    > Thanks in advance.


    You never responded to any of the messages in the other thread. But
    Chris's advice was good, and better worded than mine. Pick a problem,
    make an attempt, then ask for help.



    --

    DaveA
    Dave Angel, Nov 19, 2012
    #4
  5. On Mon, Nov 19, 2012 at 12:52 PM, su29090 <> wrote:
    > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
    >
    > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6,12, 18, 24, ... , the distance is 6.


    Each of these problems is in two halves:

    a) Understanding the mathematics behind the question
    b) Writing the code.

    Which half are you halfing (oops sorry, *having*) trouble with? If
    (a), this isn't a programming question at all - search the web for
    information on the problem, as these are well-known challenges. If
    (b), you'll need to post your code to get any sort of useful help - we
    aren't mindreaders, though we do try to look that way sometimes!

    Either way, check this out:
    http://www.catb.org/esr/faqs/smart-questions.html#homework

    ChrisA
    Chris Angelico, Nov 19, 2012
    #5
  6. On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <>
    declaimed the following in gmane.comp.python.general:

    >
    > I all of the other problems but I have issues with these:
    >
    > 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
    >

    Google: Sieve of Eratosthenes (might be mis-spelled)

    > 2.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ... , the distance is 2 while in the sequence 6, 12, 18, 24, ... , the distance is 6.
    >
    > Given the positive integer distance and the positive integer n , associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance . For example, if distance is 2 and n is 10 , then sum would be associated with 26 because 1+3+5+7+9 = 25 .
    >


    So, what have you tried?

    Consider: you have a "sum", you have a sequence of "elements" (based
    upon a spacing "distance"), and you have an upper bound "n"

    You need to generate a sequence of "elements" starting at "1", using
    "distance" as the spacing, until you exceed "n", and you want to produce
    a "sum" of all those elements...
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Nov 19, 2012
    #6
  7. On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <> declaimed
    the following in gmane.comp.python.general:


    > if is_a_prime(n):
    > is_prime = True
    >
    > Now all you have to do is write is_a_prime(). if you get stuck, please
    > show us what you've got, and what the problem is with it. And as usual,
    > tell us what version of Python you're writing in, if any.
    >

    Since "is_a_prime" is returning a Boolean, this condenses to:

    is_prime = is_a_prime(n)
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Nov 19, 2012
    #7
  8. su29090

    Dave Angel Guest

    On 11/19/2012 06:16 PM, Dennis Lee Bieber wrote:
    > On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <> declaimed
    > the following in gmane.comp.python.general:
    >
    >
    >> if is_a_prime(n):
    >> is_prime = True
    >>
    >> Now all you have to do is write is_a_prime(). if you get stuck, please
    >> show us what you've got, and what the problem is with it. And as usual,
    >> tell us what version of Python you're writing in, if any.
    >>

    > Since "is_a_prime" is returning a Boolean, this condenses to:
    >
    > is_prime = is_a_prime(n)


    Nope, the original problem statement didn't say what should should
    happen if n does have such factors. Clearly, it's only describing a
    program fragment.

    """1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

    """

    --

    DaveA
    Dave Angel, Nov 20, 2012
    #8
  9. su29090

    Ian Kelly Guest

    On Mon, Nov 19, 2012 at 4:15 PM, Dennis Lee Bieber
    <> wrote:
    > On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <>
    > declaimed the following in gmane.comp.python.general:
    >
    >>
    >> I all of the other problems but I have issues with these:
    >>
    >> 1.Given a positive integer n , assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
    >>

    > Google: Sieve of Eratosthenes (might be mis-spelled)


    No, the Sieve is nifty, but it's meant for generating sequences of
    primes, not for testing individual primality. It's also more complex
    than is necessary. A better starting place for a programming novice
    is with trial division, which is a somewhat simpler algorithm and all
    that is needed here.
    Ian Kelly, Nov 20, 2012
    #9
  10. su29090

    Neil Cerutti Guest

    On 2012-11-19, Dennis Lee Bieber <> wrote:
    > On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090
    > <> declaimed the following in
    > gmane.comp.python.general:
    >
    >>
    >> I all of the other problems but I have issues with these:
    >>
    >> 1.Given a positive integer n , assign True to is_prime if n
    >> has no factors other than 1 and itself. (Remember, m is a
    >> factor of n if m divides n evenly.)
    >>

    > Google: Sieve of Eratosthenes (might be mis-spelled)


    The sieve is a nice simple and fast algorithm, provided there's a
    bound on the highest n you need to check. It's much less simple
    and less fast if n is unbounded or the bound is unknown.

    Python's standard library isn't equipped with the an obvious
    collection to use to implement it either.

    >> 2.An arithmetic progression is a sequence of numbers in which
    >> the distance (or difference) between any two successive
    >> numbers if the same. This in the sequence 1, 3, 5, 7, ... ,
    >> the distance is 2 while in the sequence 6, 12, 18, 24, ... ,
    >> the distance is 6.
    >>
    >> Given the positive integer distance and the positive integer
    >> n , associate the variable sum with the sum of the elements
    >> of the arithmetic progression from 1 to n with distance
    >> distance . For example, if distance is 2 and n is 10 ,
    >> then sum would be associated with 26 because 1+3+5+7+9 =
    >> 25 .

    >
    > So, what have you tried?
    >
    > Consider: you have a "sum", you have a sequence of "elements"
    > (based upon a spacing "distance"), and you have an upper bound
    > "n"
    >
    > You need to generate a sequence of "elements" starting at "1",
    > using "distance" as the spacing, until you exceed "n", and you
    > want to produce a "sum" of all those elements...


    This one's sort of a trick question, depending on your definition
    of "trick". The most obvious implementation is pretty good.

    In both cases a web search and a little high-density reading
    provides insights and examples for the OP.

    --
    Neil Cerutti
    Neil Cerutti, Nov 20, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Smith

    How to achiev these two things.... ...

    Benjamin Smith, Jan 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    494
    Benjamin Smith
    Feb 1, 2005
  2. Collin VanDyck
    Replies:
    3
    Views:
    389
    Collin VanDyck
    Oct 27, 2003
  3. Rach
    Replies:
    2
    Views:
    432
    Andy Fish
    Jun 9, 2004
  4. GenxLogic
    Replies:
    3
    Views:
    1,266
    andrewmcdonagh
    Dec 6, 2006
  5. Common Man

    Please Help me solve these two questions

    Common Man, Aug 10, 2012, in forum: C Programming
    Replies:
    28
    Views:
    781
    James Kuyper
    Aug 12, 2012
Loading...

Share This Page