ARTICLE AD BOX
Why are we aiming always for the worst case since the best case is the best for all of us? What can I improve in this code?
#include <limits> #include <iostream> using namespace std; int main(int argc, char *argv[]) { /*Big O notation's theory O(n!) vs. O(n).*/ //Big O Notation is aimed for the worst scenario but we could've been aiming for the best performance possible instead. //Square root in the best case (O(n!) as worst and O(n) as the best). //Square root is the result plus n, being n as the result + 2. //Without kickstart. //Square root of 1 is 1. /*Objective (READ): 1 4 9 16 25 ... ∞ So: 1 1 + 3 = 4 (1 + aux) 4 + 5 = 9 (follow the divided table) 9 + 7 = 16 16 + 9 = 25 (We keep copying the result from a new variable and adding plus two in the result while a new result generates.) 4 (result) = 4 (result) + n (following the result + 2, which gives the result of a square root) *loop* */ int n = 1; int aux = n; //auxiliar for the result (result begins with 1) int result = 1; // for(int i = 0; i < 100; i++) { if(result == numeric_limits<int>::infinity()) { cout << "O(n)" << endl; //The code is in best case. break; } else { cout << result << endl; aux = aux + n + n; result = result + aux; } } //Result: I can't find a solution because the Big O is aiming for the best case, not the worst case. If it's aiming for the worst case, the result will be irrational, but I'm aiming for the best case and it's irrational also, although it's right. Why?' cout << "Hello world!" << endl; }This is a best case big O notation from O(n) but the algorithm aims for the worst case always, but I created a best case out of a worst case Big O thing.
