A perfect square is an integer that can be expressed as the product of two equal
ID: 3716794 • Letter: A
Question
A perfect square is an integer that can be expressed as the product of two equal integers. For example, 4 = 22 is a perfect square. An integer n is called primitive if it is divisible by no perfect square other than 1. For example, 15 is primitive but 12 is not, as 12 is divisible by 4 = 22 (a perfect square).
Write a program to find the largest even primitive integer n < t.
def factor(n):
#Primes and Exponents(pae)
pae = []
for k in range(2, math.ceil(n ** 0.5)):
if n % k == 0:
exponent = 1
while n % (k ** exponent) == 0:
exponent += 1
pae.append([k, exponent-1])
n = n // (k ** (exponent-1))
if n != 1:
pae.append([n,1])
return pae
def Isprimitive(n):
pae = factor(n)
for pair in pae:
if pair[1] != 1:
return False
else:
return True
def largestEvenPrimitive(n):
if n % 2 == 1:
n = n - 1
while (True):
if Isprimitive(n):
return n
else:
n = n - 2
print(largestEvenPrimitive(100))
this code prints 98, but I'm pretty sure is incorrect since 98 = 2 * 72.
Does this code address the question properly? is 98 the right output or can any changes be made to make it correct (if is not )?
Thank you
Explanation / Answer
def check_primitive(n):
i=2
while(i<n):
k=i**i
if(n%k==0):#if divisible by perfect squre
return False
i+=1
return True
def Largest_even_primitive(t):
t-=1
while(t>1):
if(t%2==0):#check whether t is even or odd
if(check_primitive(t)):#if even then checking whether it is primitive or not
return t#returning the largest even primitive less than t
t-=1
s = int(input("Enter a number:"))
r = Largest_even_primitive(s)
print(r)
output:
Enter a number:5
2
>>> ================================ RESTART ================================
>>>
Enter a number:15
14
>>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.