Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33519 > unrolled thread

Problems on these two questions

Started bysu29090 <129km09@gmail.com>
First post2012-11-18 17:52 -0800
Last post2012-11-19 23:32 -0700
Articles 11 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Problems on these two questions su29090 <129km09@gmail.com> - 2012-11-18 17:52 -0800
    Re: Problems on these two questions Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-19 02:09 +0000
    Re: Problems on these two questions su29090 <129km09@gmail.com> - 2012-11-18 18:15 -0800
      Re: Problems on these two questions Dave Angel <d@davea.name> - 2012-11-18 21:33 -0500
    Re: Problems on these two questions Dave Angel <d@davea.name> - 2012-11-18 21:18 -0500
    Re: Problems on these two questions Chris Angelico <rosuav@gmail.com> - 2012-11-19 13:23 +1100
    Re: Problems on these two questions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-19 18:15 -0500
      Re: Problems on these two questions Neil Cerutti <neilc@norwich.edu> - 2012-11-20 15:02 +0000
    Re: Problems on these two questions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-19 18:16 -0500
    Re: Problems on these two questions Dave Angel <d@davea.name> - 2012-11-19 20:52 -0500
    Re: Problems on these two questions Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-19 23:32 -0700

#33519 — Problems on these two questions

Fromsu29090 <129km09@gmail.com>
Date2012-11-18 17:52 -0800
SubjectProblems on these two questions
Message-ID<400c615f-4d93-4b8b-85ec-1bb4c47e30fe@googlegroups.com>
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.

[toc] | [next] | [standalone]


#33521

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-11-19 02:09 +0000
Message-ID<mailman.3801.1353290894.27098.python-list@python.org>
In reply to#33519
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.

[toc] | [prev] | [next] | [standalone]


#33523

Fromsu29090 <129km09@gmail.com>
Date2012-11-18 18:15 -0800
Message-ID<e79b3b88-36e1-4292-b7c6-a4fb0f107831@googlegroups.com>
In reply to#33519
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 is 2 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

[toc] | [prev] | [next] | [standalone]


#33529

FromDave Angel <d@davea.name>
Date2012-11-18 21:33 -0500
Message-ID<mailman.3807.1353292449.27098.python-list@python.org>
In reply to#33523
On 11/18/2012 09:15 PM, su29090 wrote:
> <snip>
> I'm using Python 3.2 

In Python 3.2, xrange has been renamed to range.  So have you tried to
solve problem #2 yet?



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#33525

FromDave Angel <d@davea.name>
Date2012-11-18 21:18 -0500
Message-ID<mailman.3803.1353291523.27098.python-list@python.org>
In reply to#33519
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

[toc] | [prev] | [next] | [standalone]


#33527

FromChris Angelico <rosuav@gmail.com>
Date2012-11-19 13:23 +1100
Message-ID<mailman.3805.1353291819.27098.python-list@python.org>
In reply to#33519
On Mon, Nov 19, 2012 at 12:52 PM, su29090 <129km09@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#33560

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-11-19 18:15 -0500
Message-ID<mailman.15.1353366936.29569.python-list@python.org>
In reply to#33519
On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <129km09@gmail.com>
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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#33625

FromNeil Cerutti <neilc@norwich.edu>
Date2012-11-20 15:02 +0000
Message-ID<ah1kbgFac8tU3@mid.individual.net>
In reply to#33560
On 2012-11-19, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090
> <129km09@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#33561

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-11-19 18:16 -0500
Message-ID<mailman.16.1353367195.29569.python-list@python.org>
In reply to#33519
On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <d@davea.name> 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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#33579

FromDave Angel <d@davea.name>
Date2012-11-19 20:52 -0500
Message-ID<mailman.27.1353376363.29569.python-list@python.org>
In reply to#33519
On 11/19/2012 06:16 PM, Dennis Lee Bieber wrote:
> On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <d@davea.name> 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

[toc] | [prev] | [next] | [standalone]


#33588

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-11-19 23:32 -0700
Message-ID<mailman.31.1353393197.29569.python-list@python.org>
In reply to#33519
On Mon, Nov 19, 2012 at 4:15 PM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <129km09@gmail.com>
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web