def amicable(num):
a = 0
for i in range(3, num):
if num % i == 0:
a = a + i
return a
for j in range(1, 500):
amicable(j)
for k in range(1, 500):
amicable(k)
if amicable(j) == amicable(k) and j != k:
print(j, 'affinity of' is', k)
I wrote a code to find the affinity water.
Here, affinities are when you add all one of the true numbers to a different number.
EX) 220, 284, and the true number of 220 is 1,2,4,5,10,11,20,22,44,55,110, plus 284. And if you add this 284 progressive factor of 1,2,4,71,142, it's 220. These numbers are called affinities
I wrote a code to get an affinity number between 1 and 500 (simply first)
The results are not coming out right. What's the problem?
First, let's point out the code you wrote.
def amicable(num):
a = 0
for i in range(3, num):
if num % i == 0:
a = a + i
return a
for j in range(1, 500):
amicable(j)
for k in range(1, 500):
amicable(k)
if amicable(j) == amicable(k) and j != k:
print(j, 'affinity of' is', k)
Here
if amicable(j) == amicable(k) and j != k:
amicable(j)
and amicable(k)
are call expressions. Looking at the shape of the whole code, I think the function call should have ended in the previous iteration. Calling the function again here is like... It seems to be written completely differently than intended.
Because at this point, j
and k
are absolutely 499. amicable(499)
is 0, which is the same as writing 0 == 0
.
The following j!=k
is 499!=499
, which also becomes a strange code that has nothing to do with the intention.
rst = []
def amicable(num):
a = 0
for i in range(3, num):
if num % i == 0:
a = a + i
if a != 0:
rst.append(a)
for i in range(1, 501):
amicable(i)
rst.sort()
print(rst)
Shouldn't we do something again after storing the sum of the Jin Yaksu separately (rst
here)?
I've done it personally, but there are a lot of things to pay attention to to reduce overlapping parts
I will try to get it in a slightly ignorant way, so I think it would be good to review it and reduce the parts that can be reduced.
def amicable(num):
a = 0
for i in range(1, num // 2 + 1):
if num % i == 0:
a += i
return a
r = []
for i in range(500):
r.append(amicable(i))
First of all, if you look at the sum of the mineral water, you can't start with the for statement 3 because the example you mentioned included 1 and 2.
And all numbers can't be a multiple of more than half that number, so you don't have to check all the numbers between 1 and the target number.
I'll tell you a slightly different way because the person above gave me a hint about saving it on the list and processing it.
x = amicable(y) &&y = amicable(x) &&x! = y
because we find the values of x and y that satisfy the values.
for i in range(500):
a = amicable(i)
if i == amicable(a) and i != a:
print(i)
You can get it this way.
You can watch it yourself, and think about what you can do to get it faster Haha
I think it can be helpful to name functions and variables in Korean when you are a beginner.
In the questioner's code, amicable
is not actually a function of finding affinity, but a function of sum of progressive numbers
.
>>> def sum of advanced numbers (n):
s = 0
for i in range(1, n):
if n%i == 0:
s += i
return s
>>> def Friendly Water Pairing (N):
Friendly water pair = []
for i in range(N+1):
s = sum of the true medicinal water (i)
if i == sum of the true medicinal water (s):
a pair of friendly waterappend((i, s))
return-friendly water pair
>>> Get Friendly Water Double (500)
[(0, 0), (6, 6), (28, 28), (220, 284), (284, 220), (496, 496)]
>>> def Friendly Water Pairing (N):
Friendly water pair = []
for i in range(N+1):
s = sum of the true medicinal water (i)
if i!= s and i == sum of the true numbers (s):
a pair of friendly waterappend((i, s))
return-friendly water pair
>>> Get Friendly Water Double (500)
[(220, 284), (284, 220)]
© 2024 OneMinuteCode. All rights reserved.