Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the recommended coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the recommended coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the recommended coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

added 3 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the officialrecommended coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the official coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the recommended coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.

Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Any ideas on how to optimize it?

You are calculating sums too many times. Sub-sequences of primes are also recalculated multiple times without reusing previous calculations.

I think using prefix sums can help here. With prefix sums you can get the sum of any sub-sequence in \$O(1)\$ time instead of \$O(N)\$ which should help.

Redundant code

This won't make a difference in the speed of your solution, but the if condition here is redundant and you can safely delete it, the range already includes that condition:

for x in range(0,len(list_of_primes)-length):
    if x+length > len(list_of_primes): return False

By the way, when a range starts from 0, you can omit the 0.

Redundant range steps

In multiple places, you use the step parameter for ranges = 1 when it's unnecessary, as the default is 1 anyway, for example here:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length,1)) :

    test_sum = sum(list_of_primes[x:x+length:1])

You could write these slightly simpler as:

    if any (list_of_primes[i] > limit/6 for i in range(x,x+length)) :

    test_sum = sum(list_of_primes[x:x+length])

Naming

@Caridorc already pointed this out, but I have to re-state it too because it's so important. The logic of this program is really not trivial, and having descriptive variable names is crucial for understanding the code.

Follow PEP8

PEP8 is the official coding style guide for Python, and your formatting violates it at multiple places. Give it a good read and follow it in the future.