I'm trying to make my code run in less than a minute.
The problem:n is an element of 1 to 100,000,000 inclusive. print the number and the largest value reached in the hailstone sequence for n only if that largest value exceeds n^2the 1st 4 lines of OP are3 167 5227 923231 9232hailstone sequence: if the 1st # is odd, the following # is 3n+1if it is even, the following # is n\2the sequence stops at 1
my code which runs in about 3 minutes:
public class Mainn
public static void main(String args)
int count = 0;
int number = 2;
ArrayList <Integer> a = new ArrayList();
while(count < 1000000)
for(int i = 2; i <= Math.sqrt(number) ; i++)
if (number%i == 0)
}//end of while
long startTime = System.currentTimeMillis();
long total = 0;
for (int i = 0; i < 1000000; i++)
total += i;
long endTime = System.currentTimeMillis();
System.out.println("Runtime = " + (endTime-startTime) + " milliseconds");
} //end of main
what do you mean caching results
ugh i just realised i posted the wrong code
this is the correct one :
public class Main
public static void findMax(int i)
int max = 0;
for (int j = 0; j<a.size(); j++)
if (a.get(j)<max && a.get(j)<Math.pow(i,2))
max = a.get(j);
public static void main (String args)
for (int i = 1; i<=7; i++)
Content will be erased after question is completed.
Enter the email address associated with your account, and we will email you a link to reset your password.
Forgot your password?