Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problems on these two questions

Reply
Thread Tools

Problems on these two questions

 
 
su29090
Guest
Posts: n/a
 
      11-19-2012

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.
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      11-19-2012
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.

 
Reply With Quote
 
 
 
 
su29090
Guest
Posts: n/a
 
      11-19-2012
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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      11-19-2012
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

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      11-19-2012
On Mon, Nov 19, 2012 at 12:52 PM, su29090 <(E-Mail Removed)> 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
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-19-2012
On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <(E-Mail Removed)>
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://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      11-19-2012
On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <(E-Mail Removed)> 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
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      11-20-2012
On 11/19/2012 06:16 PM, Dennis Lee Bieber wrote:
> On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel <(E-Mail Removed)> 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

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      11-20-2012
On Mon, Nov 19, 2012 at 4:15 PM, Dennis Lee Bieber
<(E-Mail Removed)> wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <(E-Mail Removed)>
> 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.
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      11-20-2012
On 2012-11-19, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help me solve these two questions Common Man C Programming 28 08-12-2012 11:47 AM
Two Car Insurance policies earning NCD on two cars...can these beclubbed into one next year? sherkhanthegreat@gmail.com Computer Support 13 03-27-2010 02:18 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Re: How do I translate a Chinese web site and add Chinese character set to IE..? =A0 - =A0 Taking these two questions one at a time... =A Patrick Dunford NZ Computing 3 04-28-2004 07:15 PM
Electricity Questions (does it really cause these sorts of problems?) hvrty A+ Certification 7 07-16-2003 01:50 PM



Advertisments