-1

Are there any written rules to determine runtime of a code? What would be the arithmethic actions that I have to use in outer loops and inner loops? A method in java language for example:

public static void mystery1(int n){
int k=n;// here I know that it’s O(1)
while(k>2){//O(k)
System.out.println(k);
k=(int)(Math.pow(k,1/3));
}
}

public static void dunno1(int n){
int i,j;
for(i=1;i<=n;i++){// Do I use multiplication between those loops?
   for(j=1;j<=i;j++){
       mystery1(n)n
   }
 }
}

Thank you.

  • What has your research shown? Moreover, we can expect this to be a faq. Before considering posting please read the manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. If you post a question, use one phrasing as title. Reflect your research. See [ask] & the voting arrow mouseover texts. – philipxy Apr 26 '20 at 12:11

1 Answers1

0

You can calculate the runtime of your program (java) using System.nanoTime():

long startTime = System.nanoTime();

.....your program....

long endTime   = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);

And regarding the complexity of nested for loops you can see this Answer.

Makdous
  • 1,447
  • 1
  • 12
  • 24