Which is more efficient in for loop u*u<=n or u<=sqrt(n) ?

Revision en1, by rebel_roar, 2021-06-11 00:14:35

Can anyone help me, which is more efficient while using in the for loop?

for(u = 2; u <= sqrt(n); u++)

or

for(u = 2; u*u <= n; u++)

Here are my both submission

Using first method — 119098252

Using second method — 119098208

While using the first method i got TLE but the same code using with the second method got Accepted..

I am assuming that method one is calculating sqrt(n) again and again and other one is calculating u*u again and again then why one is got accepted other one is not.

Please tell me the reason behind this?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English rebel_roar 2021-06-11 00:15:15 0 (published)
en1 English rebel_roar 2021-06-11 00:14:35 753 Initial revision (saved to drafts)