Skip to content

Commit ee6245d

Browse files
committed
eulerlib, P90, P222, P315, P345: Refactored the Python popcount() function into the shared library.
1 parent 09ce0a9 commit ee6245d

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

python/eulerlib.py

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
range = xrange
1212

1313

14+
# Returns the number of 1's in the binary representation of
15+
# the non-negative integer x. Also known as Hamming weight.
16+
def popcount(x):
17+
return bin(x).count("1")
18+
19+
1420
# Given integer x, this returns the integer floor(sqrt(x)).
1521
def sqrt(x):
1622
assert x >= 0

python/p090.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
# https://github.com/nayuki/Project-Euler-solutions
77
#
88

9+
import eulerlib
10+
911

1012
def compute():
1113
# Each die has (10 choose 6) arrangements, so we have at most 44100 arrangements to check
1214
ans = sum(1
1315
for i in range(1 << 10)
1416
for j in range(i, 1 << 10) # Ensure i <= j to force the dice to be orderless
1517
# If both have Hamming weight of 6
16-
if bin(i).count("1") == bin(j).count("1") == 6 and is_arrangement_valid(i, j))
18+
if eulerlib.popcount(i) == eulerlib.popcount(j) == 6 and is_arrangement_valid(i, j))
1719
return str(ans)
1820

1921

python/p222.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# https://github.com/nayuki/Project-Euler-solutions
77
#
88

9-
import math
9+
import eulerlib, math
1010

1111

1212
def compute():
@@ -32,7 +32,7 @@ def find_minimum_length(currentsphereindex, setofspheres):
3232

3333
# Memoization
3434
if minlength[currentsphereindex][setofspheres] is None:
35-
if bin(setofspheres).count("1") == 1:
35+
if eulerlib.popcount(setofspheres) == 1:
3636
result = sphereradii[currentsphereindex] # This sphere is rightmost
3737
else:
3838
result = float("inf")

python/p315.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def sam_transitions_minus_max_transitions(n):
2525
newstate = number_to_segments(n)
2626
if newstate == segmentstate:
2727
break
28-
maxtrans += bin(newstate ^ segmentstate).count("1")
28+
maxtrans += eulerlib.popcount(newstate ^ segmentstate)
2929
segmentstate = newstate
30-
samtrans += 2 * bin(newstate).count("1")
30+
samtrans += 2 * eulerlib.popcount(newstate)
3131
n = digit_sum(n)
32-
maxtrans += bin(segmentstate).count("1")
32+
maxtrans += eulerlib.popcount(segmentstate)
3333
return samtrans - maxtrans
3434

3535

python/p345.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
# https://github.com/nayuki/Project-Euler-solutions
77
#
88

9+
import eulerlib
10+
911

1012
def compute():
1113
# Memoization
1214
maxsum = [[None] * (2**COLUMNS) for _ in range(ROWS)]
1315

14-
def hamming_weight(x):
15-
return bin(x).count("1")
16-
1716
# Returns the maximum sum when considering the submatrix from row 'startrow' until the bottom,
1817
# with the bit set 'setofcols' indicating which column indexes are still free to be used.
1918
def find_maximum_sum(startrow, setofcols):
2019
if startrow == ROWS:
21-
assert hamming_weight(setofcols) == COLUMNS - ROWS
20+
assert eulerlib.popcount(setofcols) == COLUMNS - ROWS
2221
return 0
2322
if maxsum[startrow][setofcols] is None:
2423
result = 0

0 commit comments

Comments
 (0)