4 4 votes Let $T(n)$ denote the number of times the for loop in below code is executed on any input $n$. What can be said about $T(n)$? int iscompute(int n) { for (int i=2;i<=sqrt(n); i++) if(n%i) == 0) { printf("not computed"); return 0; } return 1; } $T(n)= O(\sqrt n)$ and $T(n)= \Omega(\sqrt n)$ $T(n)= O(\sqrt n)$ and $T(n)= \Omega(1)$ $T(n)= O( n)$ and $T(n)= \Omega (\sqrt n)$ None Algorithms go-alogrithms-1 algorithms asymptotic-notations time-complexity two-marks + – Bikram 695 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 6 6 votes int iscompute(int n) { for (int i=2;i<=sqrt(n); i++) if(n%i) == 0) { printf("not computed"); return 0; } return 1; } For loop will run sqrt(n) times at worst when $n$ is a prime number. if n is any even number then if condition becomes true in 1st iteration only. So it will terminate after that.This will result Constant best case complexity. Digvijay Pandey answered Oct 11, 2016 • selected Oct 11, 2016 by Arjun Digvijay Pandey comment Share Follow 0 reply Please log in or register to add a comment.