0,1,1,2,3,5,8,13...and so on. So, one number is the sum of the two
previous numbers, ie 5+8=13 or 1+2=3.
Something like this might help for a method. I have not tested it:
int temp=0;
int next = 1;
int previous = 0;
for(int i = 0; i < 10; i++)
{
temp = previous + next;
System.out.println(temp);
previous = next;
next = temp;
}
I've seen the Fibonacci sequence written recursively and it looks
something like this:
static int recursiveFib(int n)
{
if ( n < 0 )
return -1;
if ( n == 0)
return 0;
if ( n == 1)
return 1;
return(recursiveFib(n-1) + recursivefib(n-2));
}
I have not tested this either and it will probably trash all of your
memory as is. It needs an escape condition.
There may be a test to find a prime number in one of the java
packages, I'm not sure. Maybe Math?
I think this works for your prime numbers but again, test it.
public static void main(String[] args)
{
getPrime();
}
static void getPrime()
{
int counter =0;
int i=0;
while(counter <= 500)
{
if(isPrime(i)==true)
{
System.out.println(i);
counter++;
}//end if
i++;
}//end while
}
static boolean isPrime(int number)
{
if (number % 2==0)
return false;
for (int i = 3; i <= Math.sqrt(number); i += 2)
{
if (number % i ==0)
return false;
}//end for
return true;
}
You might be able to come up with something using a recursive GCD
(greatest common denominator) method.