218k views
1 vote
Project 5: Prime Numbers. Write a program that reads in an integer that is greater than 2 (let's call it k) and finds and prints all of the prime numbers between 3 and k. A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, ...). One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to k while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divides !n, then n must be prime. (Note that there are several easy ways to make this algorithm more efficient.)

User Griffins
by
5.2k points

2 Answers

7 votes

Answer:

#section 1

def is_prime_v1(n):

for d in range(2, n):

if n % d == 0:

return False

return True

#section 2

while True:

try:

num = int(input('Enter a number greater than 2: '))

if num > 2:

break

else:

raise

except:

print('{} is not greater than 2'.format(num))

#section 3

for n in range(3, num):

is_prime_v1(n)

a=is_prime_v1(n)

if a==True:

print (n)

Step-by-step explanation:

The programming language used is Python 3.

#section 1

In this section we create a function that will check if a number is a prime number. The function returns True for prime numbers and False for normal numbers.

#section 2

In this section we make use of a WHILE loop and a try and except block to ensure that the input by the user is greater than two, if the input is less than 2, an exception is raised and the except block prompts the user to enter the appropriate value.

when the appropriate value is entered the loop breaks and the value is carried to the last section.

#section 3

In this final section, the value collected from the user in #section 2, is inserted as the upper-limit in a range function with the lower limit set at 3.

The function created in #section 1 is called and each value in the range is passed to it and an IF statement checks if the output is True or False. If the IF block evaluated to be True, it implies that the number is a prime number and the result is printed to the screen.

User Iamtheasad
by
5.6k points
3 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int k;

cin>>k;

cout<<"2 "; //printing the first prime number.

for(int i=3;i<=k;i++)

{

int divisor=2;//divisor to check the prime numbers.

while(divisor<i)

{

if(i%divisor==0)//if i is divisible by divisor then it is not prime.

{

break;

}

divisor++;

}

if(divisor==i)

{

cout<<divisor<<" ";

}

}

return 0;

}

Input:-

24

Output:-

2 3 5 7 11 13 17 19 23

Step-by-step explanation:

The above-written code is in c++.It prints all the prime numbers upto the integer entered k.To achieve this I have nested loops for and while.For loop runs over 3 to n because 2 is a prime number so we are printing it beforehand.For every value of the for loop.It will be checked it is prime or not.If it is prime then the integer is printed else it is not printed.

User Duvet
by
4.8k points