Maruf_Hasan

SNOD

May 7th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5.  
  6.  
  7. int SNOD1(int n)//O(n) approach
  8. {
  9. int res=0;
  10. for(int i=1;i<=n;i++)
  11. {
  12. res+=n/i;
  13. }
  14. return res;
  15. }
  16.  
  17.  
  18.  
  19. int SNOD(int n)//O(sqrtN)approach
  20. {
  21. int res=0;
  22. int u=sqrt(n);
  23. for(int i=1;i<=u;i++)
  24. {
  25. res+=(n/i)-i;
  26. }
  27. res*=2;
  28. res+=u;
  29. return res;
  30. }
  31.  
  32. int main()
  33. {
  34. int n;
  35. cin>>n;
  36. int ans1=SNOD(n);
  37. int ans2=SNOD1(n);
  38. cout<<ans1<<" "<<ans2<<endl;
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment