# Off track questions (FizzBuzz, Javascript (Printing Prime Numbers) - For - loop)

Hi everyone, have you guys tried the Code Year Track?

I have a question to ask regarding FizzBuzz, The Art of Looping, Lesson 6: Free Play

I’m trying to find the answer for this question below, using for loop.

Printing all the prime numbers between 0 and 100 (this is a tricky one!)

I can’t post the question in Javascript section cuz there’s no FizzBuzz section for me to post. And I don’t want to post in wrong category, tried the search but can’t find any relevant answer except for the FizzBuzz python course (which I haven’t started yet).

Thanks @mtf, was quite lost on where to post this question.

You’re welcome. Do you have some sample code to look at?

Unfortunately, no. What I got from the hint is below:

To print out the primes, add logic to your code block that checks whether each value of counter is prime, and prints it if it is. You may need to put another for loop inside the first one!

This is a tricky one, so if you don’t see how to do it now, come back once you’ve had more practice.

I have in mind about using if & else if & else… like checking if it’s an even number, else if divisible by 3, else if divisible by 5 & 7 etc. I guess that’s the way to answer this question? But I need to read is there a better solution?

You may need to put another for loop inside the first one!

I have been cranking my mind to think of a way to put for loop in another for loop.

Maybe I don’t have better understanding yet. I started Javascript through the old Code Year track, only then to realize that there’s another two Javascript track, one old, one new.

These 3 tracks materials differ with each other. I am hoping to finish all 3 to get better grasp, but can’t wrap my head around this lesson and move on from it.

(Note: I passed that lesson, since they ask for other simple submission, but I just need an answer for my self reference, to understand the anatomy of more complex code)

Sorry for the detailed post. I need to learn to cut it down short. Thanks for your time.

There are a number of very efficient approaches for testing primeness or generating a list of primes within a given range. Rather than expound upon the best ways, let’s start with a basic brute force approach.

``````function is_prime(x) {
if (x < 2 || x % 2 == 0 && x !== 2) return false;
if (x > 2) {
for (var n = 3; n < x; n += 2) {
if (x % n == 0) return false;
}
}
return true;
}

for (var i = 0; i < 101; i++) {
if (is_prime(i)) {
console.log(i);
}
}
``````
Code
``````// Brute force primes in range 0..100

function is_prime(x) {
if (x < 2 || x % 2 === 0 && x !== 2) return false;
if (x > 2) {
for (var n = 3; n < x; n += 2) {
if (x % n === 0) return false;
}
}
return true;
}

for (var i = 0; i < 101; i++) {
if (is_prime(i)) {
console.log(i);
}
}
``````

As stated, this is brute force without consideration of optimum range or other advanced approaches. I’ll leave that for you to take up.

Notes

``````x < 2
``````

No primes less than 2.

``````x % 2 == 0 && x !== 2
``````

No even primes except 2.

``````x > 2
``````

This lets 2 slip to the return true line.

``````for (var n = 3; n < x; n += 2)
``````

Primes are odd (except 2) so we iterate only the odd numbers.

``````if (x % n == 0) return false;
``````

Primes are not divisible by anything but 1 and themselves.

On completion of the loop, the number is not divisible, so returns true.

1 Like

Here is an incentive to explore with:

``````var sieve = new Array(101);
var j, i = 101;
while (i-- > 2) {
sieve[i] = 1;
}
sieve[0] = 0;
sieve[1] = 0;
for (i = 2; i < 8; i++) {
j = i;
while (j + i < 101) {
j += i;
sieve[j] = 0;
}
}

for (i = 0; i < sieve.length; i++) {
if (sieve[i]) {
console.log(i);
}
}
``````

The above is an elementary sieve. We start with an array that has all values set to `1`, which casts to `true` in a conditional. The stack is set to all true. We then manually set the zeroth and first elements to `0`, since the cannot possibly be primes.

We’re only going up to primes up to 100 so we do not really need to re-add a number greater the closest prime to the square root of 100, which is 7. The numbers between 2 and 7, both inclusive, when sequenced over the distance to 100 will land on every index that is not prime. Note that we never start on the number, but number plus itself.

On a larger scale this may prove to have flaws, but on this problem, it fills the need. My thanks goes to @appylpye who some time ago explained this to me.

Update

Code
``````function is_prime(x) {
if (x < 2 || x % 2 === 0 && x !== 2) return false;
if (x > 2) {
for (var n = 3; n < x; n += 2) {
if (x % n === 0) return false;
}
}
return true;
}

for (var i = 0; i < 101; i++) {
if (is_prime(i)) {
console.log(i);
}
}

var sieve = new Array(101);
var j, i = 101;
while (i-- > 2) {
sieve[i] = 1;
}
sieve[0] = 0;
sieve[1] = 0;
for (i = 2; i < 8; i++) {
j = i;
while (j + i < 101) {
j += i;
sieve[j] = 0;
}
}

for (i = 0; i < sieve.length; i++) {
if (sieve[i]) {
console.log(i);
}
}
``````

Speaking of primes, we know that a limited number are all we really need. In this case, 2, 3, 5, and 7.

``````var j, i = 101, p = [2, 3, 5, 7];
while (i-- > 2) {
sieve[i] = 1;
}
sieve[0] = 0;
sieve[1] = 0;
for (i = 0; i < p.length; i++) {
j = p[i];
k = j;
while (j + k < 101) {
j += k;
sieve[j] = 0;
}
}
``````

Study closely and your precocious nature will reveal another puzzle. We don’t really need a separate list of primes since we are building one. How might this program traverse its own generated data for primes as it moves along? If it can be asked, it can be done.

1 Like

Firstly, thank you so much @mtf for the time taken to provide such in-depth answer.

Based on my progress on Code Year track currently, I’m just touching the functions in Javascript (which is the 3rd section after getting started with programming and FizzBuzz. So far, I still haven’t learn about ||, &&, !== symbol, and the return, but I believe I will encounter them as I progress further down the track.

I was a bit naive to think that the answer to my question,
(Printing all the prime numbers between 0 and 100) could be something easier/simple. Given this question was brought out in the very beginning of the track. (lol me)

Nevertheless, with your further explanation, I think I could understand the logic behind it. And the answer seem to fit in the for loop in for loop hint.

``````var sieve = new Array(101);
var j, i = 101;
while (i-- > 2) {
sieve[i] = 1;
}
sieve[0] = 0;
sieve[1] = 0;
for (i = 2; i < 8; i++) {
j = i;
while (j + i < 101) {
j += i;
sieve[j] = 0;
}
}

for (i = 0; i < sieve.length; i++) {
if (sieve[i]) {
console.log(i);
}
}
``````

I notice from the track outline, I will learn about while loop and array etc down the road. For now, I still haven’t progress till that point yet. Will refer back to this post when I’m equipped with the knowledge.

But wholly, I’m so excited to finally see some answers! and astonished as well.

Really appreciate it @mtf! Thanks!

Am hopeful to see if there’s any other answers from others too! Cheers

And, hopefully they will more intuitive as to where you are in the learning curve. Didn’t mean to get over your head. Nonetheless, set the trickier examples aside and focus on the basics. It is more important that you understand iteration and can implement it in a given situation to solve a problem. The first algorithm for primes is more along those lines and a better one to study. My apologies for not properly sensing that you were just getting started.

1 Like

Noted @mtf , I still have a lot more to cover since I’m just a beginner. However, the answer do inspire me to keep moving forward, I’m aiming to understand every portion of the code as part of my goal, like how we’d do in reading a language sentence.

Noted. It’s also somewhere nearer to my current progress.

Please, don’t be @mtf. In fact, I’m grateful to have you guiding me along the way. I should be thanking you instead. I’d rather getting replies that are above my understanding, so I can work harder on it, but still not neglecting any simple (but not efficient code) answer if there’s any.

Am glad this community exists to provide help and advice, which is very encouraging to a newb like me.

I have bookmarked this topic, will keep referring it as I progress further.

Real thanks @mtf, I could be back with more questions on these answers next time.

1 Like