Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't iterate through each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't iterate through each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't iterate through each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...
changed wording so that a user doesn't switch a while loop with a for loop and taking it as "someone said to"
Source Link
Robert Snyder
  • 3.1k
  • 2
  • 16
  • 28

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't do this with a while loop foriterate through each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't do this with a while loop for each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't iterate through each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...
Source Link

As @OldCurmudgeon said, I'd consider eliminating all numbers containing the digit 1 first, then check the primality of each. Also, don't do this with a while loop for each number, that's inefficient.

Example: The number 10000 contains a one. Since it is the first digit that is a one, you know that the next 9999 numbers also contain a one, so you can eliminate those immediately.

Do this elimination process in much faster time by simply eliminating the numbers based on the most significant digit that is a 1. At least do the first group (largest) ranges as that will save lots of time. If you don't eliminate every number with a one this way, you can still use the function you already have for the numbers you didn't eliminate this way.

This saves thousands of iterations at best!

###First digit is 1:

  • 1,
  • 10-19, 100-199, 1000-1999, 10000-19999,

###Second digit is 1:

  • 21, 210-219, 2100-2199, 21000-21999
  • 31, 310-319, ...
  • ...
  • 91, 910-919, ...

###Third digit is 1:

  • 201, 2010-2019, 20100-20199
  • ...
lang-java

AltStyle によって変換されたページ (->オリジナル) /