+ 1
Can you please help me find an error in the code
Write a program that searches among integers belonging to the numeric interval [321654; 654321] for numbers that have only odd divisors greater than 70. The divisors of 1 and the number itself are not considered. For each number found, write down the number itself and the maximal greatest divisor. Answer 405405 135135 530145 176715 592515 197505 626535 208845 My code def ch(x): l=set() for i in range (2, round(x**0.5)+1): if x%i==0: l.add(i) if all(int(c)%2 for c in l) and len(l)>70: return l return 0 for i in range (321654, 654321+1): if ch(i)!=0: print(i, max(l))
11 Answers
+ 4
Ariadna ,
since `error` can be anything, please give a clear description what the issue is.
+ 4
Ariadna ,
can you help me understand the task description:
> we are searching for numbers that have only odd divisors
and
> and these divisors should also be greater than 70
the first number (solution) that you present in your post is:
> 405405
> this number has divisors like 7, 9, 13, ... they are not greater than 70.
can you please explain this?
+ 4
Uday veer Boddh ,
please do not spam in the post of someone else. your supplied attachment has no relation to the question from the op.
+ 4
RAGNAR ,
have you compared the output of your code with the mentioned output from the op?
+ 3
Ariadna ,
thanks for giving this information, this sounds a bit different to me than the first description.
+ 1
And the number of divisors must be greater than 70, we are not comparing each to 70
+ 1
Try this :
def check_criteria(n):
max_divisor = 0
# We iterate until the square root of n for efficiency.
for i in range(2, int(n**0.5) + 1):
# If i divides n evenly, then it is a divisor.
if n % i == 0:
# Check the two potential divisors: i and n // i.
for divisor in [i, n // i]:
if divisor % 2 == 0 or divisor <= 70:
return False, None # If a divisor is even or <= 70, return False.
max_divisor = max(max_divisor, divisor) # Update the maximum divisor.
return True, max_divisor
results = []
for num in range(321654, 654321 + 1):
valid, max_div = check_criteria(num)
if valid:
results.append((num, max_div))
for r in results:
print(r)
+ 1
Ragnar, your code doesn't match the condition a bit. For example, the number 654299. Its divisors are 73, 8,963. Total divisors: 2. 2<70
But the number 626535 has 78 divisors: 3, 5, 7, 9, 13, 15, 17, 21, 27, 35, 39, 45, 51, 63, 65, 81, 85, 91, 105, 117, 119, 135, 153... And since 78>70 , the program should output the number 626535
0
You must include the maximum divisor in the answer
0
No relation ? Really ? Have a better look, please. I juste want to help not spamming ! No problem. I don't give help anymore.