HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Faster finding of all cubic numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
cubicallnumbersfasterfinding

Problem

I want to improve the performance of this program.

count = 0
store = []
n = 9238899039
for i in range(1,n):
    if i*i*i <= n:
        count += 1
        store.append(i)
print count


At the moment it is very slow. I need to find all cubic numbers up to n.

Solution

There's no reason to use brute force like that. I couldn't even get the program to complete without crashing.

To find the count, just take the cube root of n, and round down. Then, it's a simple matter to construct the list you need.

n = 9238899039
count = int(n ** (1.0/3))
store = range(1, count + 1)

Code Snippets

n = 9238899039
count = int(n ** (1.0/3))
store = range(1, count + 1)

Context

StackExchange Code Review Q#115147, answer score: 7

Revisions (0)

No revisions yet.