Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

93 lines
1.8 KiB
Markdown

---
id: 698a1a73ade5ac0e19180fa8
title: "Challenge 205: Perfect Cube Count"
challengeType: 29
dashedName: challenge-205
---
# --description--
Given two integers, determine how many perfect cubes exist in the range between and including the two numbers.
- The lower number is not guaranteed to be the first argument.
- A number is a perfect cube if there exists an integer (`n`) where `n * n * n = number`. For example, 27 is a perfect cube because `3 * 3 * 3 = 27`.
# --hints--
`count_perfect_cubes(3, 30)` should return `2`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_perfect_cubes(3, 30), 2)`)
}})
```
`count_perfect_cubes(1, 30)` should return `3`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_perfect_cubes(1, 30), 3)`)
}})
```
`count_perfect_cubes(30, 0)` should return `4`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_perfect_cubes(30, 0), 4)`)
}})
```
`count_perfect_cubes(-64, 64)` should return `9`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_perfect_cubes(-64, 64), 9)`)
}})
```
`count_perfect_cubes(9214, -8127)` should return `41`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_perfect_cubes(9214, -8127), 41)`)
}})
```
# --seed--
## --seed-contents--
```py
def count_perfect_cubes(a, b):
return a
```
# --solutions--
```py
def count_perfect_cubes(a, b):
start, end = min(a, b), max(a, b)
count = 0
n = 0
while n ** 3 <= end:
if n ** 3 >= start:
count += 1
n += 1
n = -1
while n ** 3 >= start:
if n ** 3 <= end:
count += 1
n -= 1
return count
```