Question

In: Computer Science

Write following program using Python: A positive integer greater than 1 is said to be prime...

Write following program using Python:

A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime.

Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of the prime numbers found. The other file will hold all of the composites found.

Your program must maintain two lists. One for prime numbers and another for composite numbers. For every number found to be prime, add it to your prime list. For every number found to be composite, add it to your composite list.

Create a isPrime(n) function. This function accepts a single argument named n. It returns True if n is prime. Conversely, it returns False if n is a composite number. You must use this function to make determinations.

Only after the primes and composites lists are completed, write these results to two files.

For full credit:
Good comments.
Your program should not crash on errors.
Must work for any number n.
Your program maintains 2 lists. One of Primes and one of Composite numbers.
Your program creates 2 output files. One of Primes and one of Composite numbers.
Be sure to demonstrate good input validation.

Upload your ORIGINAL work to BlackBoard before the due date and time. No google, cheg, 'collaborations', or any unoriginal work will be accepted.

Consider the following for finding prime numbers...

def prime_or_composite(n):
has_divisor = False
for i in range(2, n):
if n % i == 0:
has_divisor = True
if has_divisor:
print(n, 'is composite.')
else:

print(n, 'is prime.')

Solutions

Expert Solution

def isInt(n):

try:

int(n)

return True

except:

return False

def prime_or_composite(n):

has_divisor = False

for i in range(2, n):

if n % i == 0:

has_divisor = True

if has_divisor:

print(n, 'is composite.')

else:

print(n, 'is prime.')

return has_divisor

n = input("Enter a number: ")

if isInt(n):

composites = []

primes = []

for i in range(2, int(n)+1):

if not prime_or_composite(i):

primes.append(str(i))

else:

composites.append(str(i))

f = open("primes.txt", "w")

data = " ".join(primes)

f.write(data)

f.close()

f = open("composites.txt", "w")

data = " ".join(composites)

f.write(data)

f.close()

else:

print("Provided invalid input.")

''' OUTPUT

Enter a number: 100

2 is prime.

3 is prime.

4 is composite.

5 is prime.

6 is composite.

7 is prime.

8 is composite.

9 is composite.

10 is composite.

11 is prime.

12 is composite.

13 is prime.

14 is composite.

15 is composite.

16 is composite.

17 is prime.

18 is composite.

19 is prime.

20 is composite.

21 is composite.

22 is composite.

23 is prime.

24 is composite.

25 is composite.

26 is composite.

27 is composite.

28 is composite.

29 is prime.

30 is composite.

31 is prime.

32 is composite.

33 is composite.

34 is composite.

35 is composite.

36 is composite.

37 is prime.

38 is composite.

39 is composite.

40 is composite.

41 is prime.

42 is composite.

43 is prime.

44 is composite.

45 is composite.

46 is composite.

47 is prime.

48 is composite.

49 is composite.

50 is composite.

51 is composite.

52 is composite.

53 is prime.

54 is composite.

55 is composite.

56 is composite.

57 is composite.

58 is composite.

59 is prime.

60 is composite.

61 is prime.

62 is composite.

63 is composite.

64 is composite.

65 is composite.

66 is composite.

67 is prime.

68 is composite.

69 is composite.

70 is composite.

71 is prime.

72 is composite.

73 is prime.

74 is composite.

75 is composite.

76 is composite.

77 is composite.

78 is composite.

79 is prime.

80 is composite.

81 is composite.

82 is composite.

83 is prime.

84 is composite.

85 is composite.

86 is composite.

87 is composite.

88 is composite.

89 is prime.

90 is composite.

91 is composite.

92 is composite.

93 is composite.

94 is composite.

95 is composite.

96 is composite.

97 is prime.

98 is composite.

99 is composite.

100 is composite.

SAMPLE OUTPUT FILES

primes.txt: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

composites.txt: 4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40 42 44 45 46 48 49 50 51 52 54 55 56 57 58 60 62 63 64 65 66 68 69 70 72 74 75 76 77 78 80 81 82 84 85 86 87 88 90 91 92 93 94 95 96 98 99 100

'''

# ----- Up vote or comment. Happy Learning!


Related Solutions

A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime number.
In Python, write a program to get a positive integer n from keyboard first (your program...
In Python, write a program to get a positive integer n from keyboard first (your program must be able to check the validation of n being positive), and then get n integers from keyboard and store these n integers in a list. Do some processing, and print out average of the elements in the list at end of your program. For example, suppose user enters 3 first (n = 3), which means we get another three integers from the user....
1) How many positive integers are greater than 140, less than 30800 and relatively prime to...
1) How many positive integers are greater than 140, less than 30800 and relatively prime to 280.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT