ZEN_OF_PYTHON Telegram 4494
Вопрос подписчика

Задает @StSav012:

«Предлагаю задачку на алгоритм.
Для данных n ∈ ℕ и чётного N ≥ n > 1, N ∈ ℕ, найти чётное ñ, ближайшее к n, являющееся делителем N. Если таких чисел несколько, выбрать наибольшее.Конечно, хочется скорость O(log(n)).


def find_closest_even_divisor(n: int, max_n: int) -> int:
"""
For given n ∈ ℕ and an even N ≥ n, N ∈ ℕ,
find an even ñ closest to n so that ñ is a divisor of N.

If there are several such numbers, pick the largest one.
"""

if max_n <= 0:
raise ValueError(f"There are no positive divisors for {max_n}")
if max_n & 1:
raise ValueError(f"There are no even divisors for {max_n}, which is odd")

if n > 0.75 * max_n:
return max_n

if max_n % n == 0 and n & 1 == 0:
return n

divisors: list[int] = []
dd: int = 1
while max_n > 1 and max_n & 1 == 0:
max_n >>= 1
dd <<= 1
divisors.append(dd)

d: int = 3
dc: list[int]
while max_n > 1:
dc = []
dd = 1
while max_n % d == 0:
max_n //= d
dd *= d
dc.append(dd)
else:
if dc:
divisors += [divisor * dd for divisor in divisors for dd in dc]
if d < isqrt(max_n) and d < 2 * n:
d += 2
else:
break

return min(divisors, key=lambda _d: (abs(n - _d), -_d))

Что ещё можно ускорить?»

NB! Пожалуйста, будьте взаимовежливы. Однажды и вам помогут в этой рубрике.

#обсуждение
@zen_of_python
1



tgoop.com/zen_of_python/4494
Create:
Last Update:

Вопрос подписчика

Задает @StSav012:

«Предлагаю задачку на алгоритм.
Для данных n ∈ ℕ и чётного N ≥ n > 1, N ∈ ℕ, найти чётное ñ, ближайшее к n, являющееся делителем N. Если таких чисел несколько, выбрать наибольшее.Конечно, хочется скорость O(log(n)).


def find_closest_even_divisor(n: int, max_n: int) -> int:
"""
For given n ∈ ℕ and an even N ≥ n, N ∈ ℕ,
find an even ñ closest to n so that ñ is a divisor of N.

If there are several such numbers, pick the largest one.
"""

if max_n <= 0:
raise ValueError(f"There are no positive divisors for {max_n}")
if max_n & 1:
raise ValueError(f"There are no even divisors for {max_n}, which is odd")

if n > 0.75 * max_n:
return max_n

if max_n % n == 0 and n & 1 == 0:
return n

divisors: list[int] = []
dd: int = 1
while max_n > 1 and max_n & 1 == 0:
max_n >>= 1
dd <<= 1
divisors.append(dd)

d: int = 3
dc: list[int]
while max_n > 1:
dc = []
dd = 1
while max_n % d == 0:
max_n //= d
dd *= d
dc.append(dd)
else:
if dc:
divisors += [divisor * dd for divisor in divisors for dd in dc]
if d < isqrt(max_n) and d < 2 * n:
d += 2
else:
break

return min(divisors, key=lambda _d: (abs(n - _d), -_d))

Что ещё можно ускорить?»

NB! Пожалуйста, будьте взаимовежливы. Однажды и вам помогут в этой рубрике.

#обсуждение
@zen_of_python

BY Zen of Python


Share with your friend now:
tgoop.com/zen_of_python/4494

View MORE
Open in Telegram


Telegram News

Date: |

In 2018, Telegram’s audience reached 200 million people, with 500,000 new users joining the messenger every day. It was launched for iOS on 14 August 2013 and Android on 20 October 2013. “[The defendant] could not shift his criminal liability,” Hui said. Telegram iOS app: In the “Chats” tab, click the new message icon in the right upper corner. Select “New Channel.” The SUCK Channel on Telegram, with a message saying some content has been removed by the police. Photo: Telegram screenshot. As five out of seven counts were serious, Hui sentenced Ng to six years and six months in jail.
from us


Telegram Zen of Python
FROM American