Jayeemsuh Allen wrote:My guess is that the problem is with my nested 'if' statement.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
.Are either: Prime, or Multiples of 3, or 8, but not both 3 and 8, less than 2500
Jayeemsuh Allen wrote:So I just did the Sq. Root of 15 on a calculator and I get ~3.8. Then "(int)" forces it to the integer 3. Isn't this the optimal way to test for primes?
As for being divisible by 3 AND 8, I don't the wording of the challenge suggests that...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Test your program. I can't stress enough how important it is to do that.
Jayeemsuh Allen wrote:I was struggling with how to discern whether a number is prime or not while keeping the for loop going to try all divisors. I'm not sure if this is the most efficient way,...
...but I created a counter variable for number of divisors and then added my if statement after the for loop to print the prime number as long as numDivisors == 0. This seems to print the desired numbers.
I'm interpreting the wording of the challenge to mean "print all multiples of 3 and all multiples of 8, omit numbers that are multiples of 3 and 8" I believe the code does this. Adding: was an experiment but it seems to work. I don't totally understand why though.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I believe the code does this. Adding:
if (i % 3 != 0 || i % 8 != 0) {
was an experiment but it seems to work. I don't totally understand why though. Is it because the || operator is exclusive from the && operator? Does that make sense?
Are you better than me? Then please show me my mistakes..
Supun Lakshan Dissanayake wrote:following for-loop will present prime numbers between 0 and 2500. so why did you struggle with Math.sqrt(i); ?
I guess this is the most efficient way to get primes.
which shows 3 false values in the first 12 results (as well as missing out a few)1 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
25 is a prime number
29 is a prime number
31 is a prime number
35 is a prime number
...
Tony Docherty wrote:
Supun Lakshan Dissanayake wrote:following for-loop will present prime numbers between 0 and 2500. so why did you struggle with Math.sqrt(i); ?
I guess this is the most efficient way to get primes.
No it won't give only prime numbers.
If you run your code you will get:
which shows 3 false values in the first 12 results (as well as missing out a few)1 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
25 is a prime number
29 is a prime number
31 is a prime number
35 is a prime number
...
Are you better than me? Then please show me my mistakes..