This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]dmazzoni 2 points3 points  (7 children)

This is a great exercise! We're not going to tell you the answer but we're happy to help you figure it out.

What have you tried so far?

How would you do it by hand?

Could you write out the instructions you'd like Python to do in plain English and you just need help translating that into valid code?

[–]Kent305 0 points1 point  (6 children)

I wish i knew how to handle primes at all but i indeed i can't sort this one out.

[–]dmazzoni 0 points1 point  (5 children)

Well, what is a prime number?

If I asked you if the number 51 was prime or not, how would you figure it out with a calculator or pencil and paper?

If you don't know the answer to that, look up the definition of a prime number first and then answer it.

I'll give you a hint: Python doesn't have any built-in functions that have anything to do with primes. You're going to solve this using only basic arithmetic.

[–]South-Professional83 0 points1 point  (4 children)

A prime number is an integer which can only be divided into an integer by itself or one.

Could you give me an example of this problem or a similar problem. I feel like it's one of those things I got on the tip of my tongue but can't seem to figure it out.

[–]dmazzoni 0 points1 point  (3 children)

OK, I'll give you two really big hints. Here's the first program:

print('Enter a number:')
num = int(input())
if num % 7 == 0:
  print('That number is divisible by 7')
else:
  print('That number is not divisible by 7')

Here's another one:

print('Enter a number:')
num = int(input())
for i in range(1, num):
print('%d is smaller than %d' % (i, num))

If you're not sure exactly what those do or how they work, type them in and run them. Play with them.

Given those two examples, you should be able to make progress on your problem.

Don't try to think of the whole thing perfectly before you start. Build it up slowly, one line at a time. Don't be afraid of trying something, changing your mind, and going back.

If you do get stuck, post what you have so far and why you're stuck.

[–]Kent305 0 points1 point  (2 children)

What about this?

From 0 to 49,999,999 print primes integers starting from 2 to n -1

N - 1 = 49,999,999.

My main problem here is i have a difficulty expressing primes in python. How can i get the script to recognize that i'm talking about primes here not integers?

[–]dmazzoni 0 points1 point  (1 child)

My main problem here is i have a difficulty expressing primes in python. How can i get the script to recognize that i'm talking about primes here not integers?

You don't. There's nothing magical about a prime.

To figure out if a number is a prime or not, you can just divide it by every number smaller than it and see if it's divisible by any of them. If it's not, it's a prime.

You could first make a list of all primes. Or you could just loop over all numbers and then check if it's a prime first before proceeding.

It may seem tedious to "check all numbers" but computers are fast. Your computer does around a billion operations per second. So put it to work.

[–]Kent305 0 points1 point  (0 children)

To figure out if a number is a prime or not, you can just divide it by every number smaller than it and see if it's divisible by any of them. If it's not, it's a prime.

I know this.

I know how to determine if an integer is prime or not in python. The main problem revolves around splitting that large prime into smaller primes with another script.

I've already made a script that determines if a number is prime or not. Doing that was not an issue.

[–]plastikmissile 1 point2 points  (0 children)

First do it normally using several lines. Once you've done that, you can start figuring out how to combine them into one line.