+ 2
Square root
Hi everyone! I have a question. I need a program that prints the square root of a number without all the decimals. For example if I have the square root of 19 that is 4.3588... I only want the 4. Thank you:)
49 Réponses
+ 1
Try Math.floor(Math.sqrt(19))
+ 1
I’m new at programming and I have this homework that I think is a little difficult. I’m trying to do every separately but I’m having a lot of trouble. Do you have time so I can ask you more questions?
+ 1
Yes
+ 1
You didn't understand well, 4 is not prime
+ 1
Do you know what is the prime number? So I can continue explaining you?
+ 1
So prime number is something that is divisible only by 1 and itself. Do you understand?
+ 1
Ok, I can write you the answer of the exersize, but I want you to understand
+ 1
So do you undestand what is the prime number
+ 1
Ok so to check if 13 is prime we need to say to program to check if it is divisible by 2, 3, 4, 5, 6, 7, 8, 9,10,11,12 and if it is not divisible by these numbers the number is prime. Do you understand?
+ 1
Now imagine how much numbers should computer check if we want to check if 1000000007 is prime. It will take a lot of time, yes?
+ 1
So mathematicians finds that we don't need to check every number we can check only until square root of the number. For example 19, we need to check only 2, 3, 4 if are divisible, we don't need to check every number until 19
+ 1
That will save a lot of time when we want to check big numbers like 1000000007, because we will check only until aproximatly 30000
+ 1
Here is the code
https://code.sololearn.com/cx0ri0xJRl6z/?ref=app
+ 1
This is very optimized code, you can change n value by everything you want
+ 1
I am looping to i*i<=n which is mathematicaly the same as i<=Math.sqrt(n), but sqrt function is slower than multiplication. As you can see I am incrementing i+=2, because I check only odd numbers, because the only one even prime number is 2.
+ 1
oh, in exersice is writen first n number, not numbers to n-what I made. Now I will fix
+ 1
Fixed it.
+ 1
if you have questions, ask me I am glad to help you.
+ 1
Do you succeed
+ 1
As I understand we need to print first 'n' numbers, not the numbers until 'n'