Search: | |||||

| ||||

Home » Forum » Java | RSS Feeds |

I'm new to Java and trying to write some java code that will accept 2

numbers to start off my Fibonacci list and generate a list of 50 and

print them on screen. I'm also trying to generate some java code that

will generate 500 Prime Numbers.

Anyone got any ideas?

Read some books a lot of them outthere with the

similar idea.

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.

Didn't find what you were looking for?
Find more on Fibonacci and Prime numbers
Or get search suggestion and latest updates.

Related Topics:

- prime number
- prime number problem
- prime number
- find prime no. using thread
- Prime Nos
- How is inserting fibonacci series in singly linked list done?
- Converting a "Stringed" Number to a Number
- Converting a "Stringed" Number to a Number
- triangle of numbers,:-( i owe you one
- C program that takes Salesperson details such as Payroll number, Surname, Other Names, Basic Sa
- perfect number
- perfect number
- Read integer number
- Display character which appears number of time equal half of maximum time
- sum of series of natural numbers
- Get Column by number
- I would like to fax directly from a fax number in a cell
- Delete everything in a workbook that is a number
- combine number formats
- Subs with a non-predetermined number of arguments?
- Discover number format
- Converting a number to a sentence
- Routine needed for continuing numbers.
- Change every second column to a negative number
- Number Format