Write a program using Python which gets a natural number n, and classifies the r
ID: 669843 • Letter: W
Question
Write a program using Python which gets a natural number n, and classifies the range of numbers (natural numbers less that n), with respect to perfect, abundant or deficient.
Use two while loop
Rewrite the program replacing the two while loops with two for loops and range.
Explanation / Answer
# This file contains python code that will determine whether a potivie range # of integers entered by the user will contain odd or even numbers, prime or # composite numbers, perfect/abundant/deficient numbers, square numbers, and # triangular numbers. def main(): import math firstNum = 0 secondNum = 0 n = 0 odd = 0 prime = 0 perfect = 0 square = 0 perfect = 0 square = 0 triangular = 0 printGreeting() firstNum = input('Please enter an integer between 1 and 100000: ') secondNum = input('Please enter an interger greater than your pervious but not larger than 100000: ') for n in range(firstNum, secondNum + 1): isOdd(n) isPrime(n) checkForPerfect(n) isSquare(n) isTriangular(n) printTablLine(n, odd, prime, perfect, square, triangular) main() printGreeting(): print 'This program classifies positive integers as Odd/Even,' print 'Prime/Composite, Perfect/Abundant/Deficient, Square,' print 'and Triangular.' print print 'You will now get to choose the range of positive intergers' print 'that you would like to see classified.' print def printTableHeading(): def isOdd(n): if firstNum % 2 == 0: return False else: return True def isPrime(n): if n == 2: return True for divisor in range(3, int(math.sqrt(n)), 2): if n % divisor == 0: return False return True def checkForPerfect(n): sum = 0 if n % == 0: sum += if sum == n: return 'Perfect' elif sum > n: return 'Abundant' else: return 'Abundant' else: return 'Deficient' def sumDivisors(n): # returns the sum of the divisors of n. def isDivisor(a, b): # a predicate function that returns True if b is a divisor of a, and False if not. def isSquare(n): # returns True if n is a perfect square, False if not. def isTriangular(n): # returns True if n is a triangular number, False if not. def printTableLine(n, odd, prime, perfect, square, triangular): # prints the information for one number on one line of the table.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.