Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Problems on these two questions (http://www.velocityreviews.com/forums/t954614-problems-on-these-two-questions.html)

su29090 11-19-2012 01:52 AM

Problems on these two questions
 

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.

Mark Lawrence 11-19-2012 02:09 AM

Re: Problems on these two questions
 
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.


su29090 11-19-2012 02:15 AM

Re: Problems on these two questions
 
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

Dave Angel 11-19-2012 02:18 AM

Re: Problems on these two questions
 
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


Chris Angelico 11-19-2012 02:23 AM

Re: Problems on these two questions
 
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-q....html#homework

ChrisA

Dennis Lee Bieber 11-19-2012 11:15 PM

Re: Problems on these two questions
 
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/


Dennis Lee Bieber 11-19-2012 11:16 PM

Re: Problems on these two questions
 
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/


Dave Angel 11-20-2012 01:52 AM

Re: Problems on these two questions
 
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


Ian Kelly 11-20-2012 06:32 AM

Re: Problems on these two questions
 
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.

Neil Cerutti 11-20-2012 03:02 PM

Re: Problems on these two questions
 
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


All times are GMT. The time now is 03:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.