Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, let's debug exercise number three.
So in this exercise, it's supposed to output a random number between one and 15 printed and then print
whether that number is prime or composites.
Now, prime means that the number is only divisible by itself and one.
So numbers like 1117 are 43.
But if we run the code.
Clearly it doesn't work.
And we're going to use breakpoints to visualize the runtime and debug the issue.
And there's your first mistake.
It returns 16 when it should return.
Year enemy number between one and 15.
But why is the code crashing?
Let's not worry about that right now.
There's clearly a bug right here, so let's step into the function.
And now it's perfectly clear what the error was.
The range that comes in is 15 plus one, plus that random decimal, which is going to be 16 point something.
And then casting that to an integer is always going to return 16.
So clearly this is all wrong.
Instead, we should multiply the range by the random value that gets returned and then add one.
All right, let's try this again.
Replay the debugger.
Much better.
We can keep testing this out and it keeps returning a random number each time.
Okay.
That number gets stored in a variable named random and gets printed.
Okay.
Now the crash happens in this function, so we'll step into it.
First, it's going to go inside the print line function.
We don't care about that.
So step out of it.
And this is the last line of code.
So I'm assuming here is where it crashes.
Okay.
What's going on?
We have a random number coming in.
And that number modulus.
Some value is causing the crash.
Ah, that makes sense.
As you know by now.
Modulus means dividing two numbers and returning the remainder, and you can never divide a number by
zero.
That's impossible, which causes an arithmetic exception.
And so what we want to do is start the loop counter at one, none at zero.
All right, let's try this out.
Wait.
This number is not a prime number.
There's something wrong.
Let's restart the debugger and go through the loop.
Okay.
So this number should be a composite.
And the remainder from dividing this number by one and zero, which would make it composite.
But wait a second.
Every number is divisible by one, so it doesn't really make sense to start the loop at one.
Anyways, let's keep going.
It is divisible by two into the fact that it's divisible by two means.
We know this number is composite, so we don't need to keep going with the loop.
But the loop keeps going and because it's not divisible by three, it marks it back as prime, which
doesn't make any sense.
The first thing we need to do is break the loop as soon as we establish that the random number is composites.
All right, relaunch the debugger.
Okay.
This time you get a number that's clearly prime.
And remember the rule that you can't divide a prime number by anything except itself in one.
But wait, our code isn't going to work because the loop starts at one and it's going to think, okay,
this number divided by one returns a remainder of zero.
So clearly this number is composite and it's going to break the loop.
And here you can see that happen in real time.
This number is not a composite number, and dividing it by one should not be a criteria to determine
if a number is prime or composites.
So we need to start the loop off at two.
All right, let's try this again.
Now we get a composite number.
That number is indeed divisible by two.
So it's going to return a remainder of zero, which means it's composite.
Break the loop and we're good.
All right, let's keep running tests.
Ken.
Now we've got a prime number.
That number is not divisible by anything.
So it's going to keep going through the entire loop and none of the numbers are going to return a non-zero
modulus.
And since the number is not divisible by anything, it's always going to be prime.
And that's all.
Now I would like to rewrite this code.
A more efficient way to do this is to start the variable office prime.
And if the numbers happen to be divisible by one of the numbers inside the range.
Then we're going to label it as composite and break the loop.
To me, that makes a lot more sense.
So let's rerun and visualize the runtime.
Okay.
Now we're going to composite number.
First we assume that it's prime.
That number is indeed divisible by two.
So this condition is going to be true, making it composite.
Breaking the loop and returning the results.
All right.
This time you get a prime number.
First we assume it's prime.
It's not going to divide by anything.
So it will stay prime.
Eventually the loop is going to break and return a prime result.
And the one last thing is that the number one is not a prime number.
So in the event that the random number returned is one, this function is actually going to return prime,
which is incorrect.
So here what I can do is use the ternary operator to say if random value equals one.
Otherwise we'll pass that number into the is prime function and then it returned the correct results.
All right.
One more time.
Number is one.
What a coincidence.
This evaluates the true.
This time.
This evaluates the files and it's going to run the function which determines whether the value is prime
or composite.
In this case, it ends up being composites.
By the way, you don't have to use the ternary operator if you're not comfortable with it.
You can use standard default if you want.
It's the same thing.
All right.
That's all.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.