Bei einer positiven Ganzzahl n können wir alle Gitterpunkte im Quadrat n x n bezeichnen. Beginnend bei 1, die Gesamtzahl der Gitterpunkte ist N x n und die Gitterpunkte sind Liste (Itertools.Product (Bereich (1, n + 1), Wiederholung = 2)) . Nicht reduzierte Fraktion ist Folgendes ist eine Bruteforce-Implementierung, die garantiert korrekt ist, aber sehr ineffizient ist: < /p>
import math
from itertools import product
def find_complex_points(lim: int) -> list[tuple[int, int]]:
return [
(x, y)
for x, y in product(range(1, lim + 1), repeat=2)
if math.gcd(x, y) > 1
]
< /code>
Jetzt ist die nächste Funktion etwas schlauer, aber sie erzeugt Duplikate und als Ergebnis nur merklich schneller, aber nicht viel: < /p>
def find_complex_points_1(lim: int) -> set[tuple[int, int]]:
lim += 1
return {
(x, y)
for mult in range(2, lim)
for x, y in product(range(mult, lim, mult), repeat=2)
}
< /code>
In [255]: %timeit find_complex_points(1024)
233 ms ± 4.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [256]: %timeit find_complex_points_1(1024)
194 ms ± 1.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
< /code>
Is there a better way to accomplish this?
(My goal is simple, I want to create a NumPy 2D array of uint8 type with shape (N, N), fill it with 255, and make all pixels (x, y) 0 if (x+1)/(y+1) is a non-reduced fraction)
I have devised a method that is smarter than both my previous ones by a wide margin, and also tremendously faster, but it still generates duplicates, I have opt to not to use a set
hier, damit Sie den Code so kopieren können, wie es ist, und einige Tests ausführen und die genaue Ausgabe in der Reihenfolge sehen, die sie generiert werden:
Bei einer positiven Ganzzahl n können wir alle Gitterpunkte im Quadrat n x n bezeichnen. Beginnend bei 1, die Gesamtzahl der Gitterpunkte ist N x n und die Gitterpunkte sind Liste (Itertools.Product (Bereich (1, n + 1), Wiederholung = 2)) . Nicht reduzierte Fraktion ist Folgendes ist eine Bruteforce-Implementierung, die garantiert korrekt ist, aber sehr ineffizient ist: < /p> [code]import math from itertools import product
def find_complex_points(lim: int) -> list[tuple[int, int]]: return [ (x, y) for x, y in product(range(1, lim + 1), repeat=2) if math.gcd(x, y) > 1 ] < /code> Jetzt ist die nächste Funktion etwas schlauer, aber sie erzeugt Duplikate und als Ergebnis nur merklich schneller, aber nicht viel: < /p> def find_complex_points_1(lim: int) -> set[tuple[int, int]]: lim += 1 return { (x, y) for mult in range(2, lim) for x, y in product(range(mult, lim, mult), repeat=2) } < /code> In [255]: %timeit find_complex_points(1024) 233 ms ± 4.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [256]: %timeit find_complex_points_1(1024) 194 ms ± 1.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) < /code> Is there a better way to accomplish this? (My goal is simple, I want to create a NumPy 2D array of uint8 type with shape (N, N), fill it with 255, and make all pixels (x, y) 0 if (x+1)/(y+1) is a non-reduced fraction)
I have devised a method that is smarter than both my previous ones by a wide margin, and also tremendously faster, but it still generates duplicates, I have opt to not to use a set[/code] hier, damit Sie den Code so kopieren können, wie es ist, und einige Tests ausführen und die genaue Ausgabe in der Reihenfolge sehen, die sie generiert werden: [code]def find_complex_points_2(lim: int) -> set[tuple[int, int]]: stack = dict.fromkeys(range(lim, 1, -1)) lim += 1 points = [] while stack: x, _ = stack.popitem() points.append((x, x)) mults = [] for y in range(x * 2, lim, x): stack.pop(y, None) mults.append(y) points.extend([(x, y), (y, x)])
for i, x in enumerate(mults): points.append((x, x)) for y in mults[i + 1:]: points.extend([(x, y), (y, x)])
Bei einer positiven Ganzzahl n können wir alle Gitterpunkte im Quadrat n x n bezeichnen. Beginnend bei 1, die Gesamtzahl der Gitterpunkte ist N x n und die Gitterpunkte sind Liste (Itertools.Product...
Ich habe ein Sinussignal, das durch zwei Listen definiert ist
Bildbeschreibung hier eingeben. voltage = conductance = edges = [] for n in voltage: if n == 0: pass elif voltage > 0.5 and voltage -35....
Ich möchte die Intervallschätzung der Bevölkerungsstandardabweichung berechnen. Dafür brauche ich die kritischen Chi-Quadrat-Werte für die untere und obere Grenze. Die zu berechnende Formel ist dies...
Ich habe dieses nervige Problem in der Sympy gefunden. Noch frustrierender.
import sympy as sp
a,b,c,d = sp.symbols('a, b, c, d', positive = True)
expr = (a*b*c+2*d*a*c+b*c*d)/(2*a+b)
correct_simpl...
Ich lese Excel-Tabellen über Python und versuche, nur sichtbare Zeilen in Python zu lesen (nicht ausgeblendet oder reduziert). Ich habe die Dokumentation von OPENPYXL durchgesehen und festgestellt,...