+ 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:)

12th Apr 2018, 9:44 PM
Fanny Torres Gonzalez
Fanny Torres Gonzalez - avatar
49 Respuestas
+ 1
Try Math.floor(Math.sqrt(19))
12th Apr 2018, 9:48 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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?
12th Apr 2018, 9:54 PM
Fanny Torres Gonzalez
Fanny Torres Gonzalez - avatar
+ 1
Yes
12th Apr 2018, 9:55 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
You didn't understand well, 4 is not prime
12th Apr 2018, 10:02 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
Do you know what is the prime number? So I can continue explaining you?
12th Apr 2018, 10:04 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
So prime number is something that is divisible only by 1 and itself. Do you understand?
12th Apr 2018, 10:08 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
Ok, I can write you the answer of the exersize, but I want you to understand
12th Apr 2018, 10:23 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
So do you undestand what is the prime number
12th Apr 2018, 10:26 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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?
12th Apr 2018, 10:34 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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?
12th Apr 2018, 10:39 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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
12th Apr 2018, 10:43 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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
12th Apr 2018, 10:45 PM
Vladi Petrov
Vladi Petrov - avatar
12th Apr 2018, 10:51 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
This is very optimized code, you can change n value by everything you want
12th Apr 2018, 10:52 PM
Vladi Petrov
Vladi Petrov - avatar
+ 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.
12th Apr 2018, 10:55 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
oh, in exersice is writen first n number, not numbers to n-what I made. Now I will fix
12th Apr 2018, 11:01 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
Fixed it.
12th Apr 2018, 11:09 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
if you have questions, ask me I am glad to help you.
12th Apr 2018, 11:14 PM
Vladi Petrov
Vladi Petrov - avatar
+ 1
Do you succeed
13th Apr 2018, 5:48 AM
Vladi Petrov
Vladi Petrov - avatar
+ 1
As I understand we need to print first 'n' numbers, not the numbers until 'n'
13th Apr 2018, 5:50 AM
Vladi Petrov
Vladi Petrov - avatar