Problems on these two questions

S

su29090

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.
 
M

Mark Lawrence

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.
 
S

su29090

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
 
D

Dave Angel

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.
 
C

Chris Angelico

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
 
D

Dennis Lee Bieber

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...
 
D

Dennis Lee Bieber

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)
 
D

Dave Angel

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.)

"""
 
I

Ian Kelly

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.
 
N

Neil Cerutti

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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top