ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

Edit: One way you could go about it is first calculate your prime numbers; then calculate your fibonacci sequence which is not less than your highest calculated prime number; and finally create a new list iterating the prime numbers. If the prime number iterated is in the fibonacci sequence list then append it to a new list. Output that new list at the end when all sequences are done.

It looks good for the prime checking, the problem is the fibonacci part.

Try something like this (it's your own code with a couple of comments, and cleaned up a whole lot - you'll need to fill in the while loop, we're not doing your homework for you ):

Code:

#include<stdio.h>
int main(){
int n, c;
printf("Enter a number to check if it is prime and fibonacci\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ ){ //not optimal, but simple
if ( n%c == 0 ){
printf("%d is not prime.\n", n);
return 0;
}
}
//so here we know n is prime
int a = 0;
int b = 1;
while(a+b <= n){
//fill in the loop to test if it's fibonacci
}
printf("%d is both prime and in the fibonacci sequence",n);
return 0;
}

You were broadly correct in your first approach, just lots of extra variables and you got a bit confused over the fibonnaci part.

Since you've got it. Here's a fun example showing the first 8 numbers which are both prime and in the fibonacci series using Python. I was just playing around since we were on the topic .

Code:

#!/usr/bin/env python
#output the first 8 numbers which are both prime and in fibonacci sequence
count=1
#initialize fibonachi numbers
(a,b)=(0,1)
fibonacci_numbers=[0,1]
#initialize prime numbers
current_num = 3
prime_numbers = [2]
#initialize a list for both prime and fibonnaci series numbers
both_prime_and_fibonacci = [2]
print "Calculating primes and fibonacci series (may take a while)..."
while count < 8:
is_prime = True
#check if the number is prime
for item in prime_numbers:
if current_num%item == 0:
is_prime = False
if is_prime:
#is prime so add to prime number list
prime_numbers.append(current_num)
#grow the list of fibonacci numbers so that it is not less than the largest prime number
while fibonacci_numbers[len(fibonacci_numbers)-1] < current_num:
(a,b) = (b,a+b)
fibonacci_numbers.append(a)
#check to see if prime number is in the fibonacci sequence
if current_num in fibonacci_numbers:
both_prime_and_fibonacci.append(current_num)
count = count+1
#increment to the next odd number to be tested if prime
current_num = current_num+2
#display the results
for item in both_prime_and_fibonacci:
print item
#fibonacci sequence test
#(a,b)=(0,1)
#print "a= %d" % a
#for i in range(0,10):
# (a,b)=(b,a+b)
# print "a= %d" % a

You can check the counter only till the sqrt of n for a prime no. That makes it more faster.

Code:

for(c=2;c<sqrt(n);c++){
...
}

Certainly no need to overwork the loop beyond the square root. But the overhead of the sqrt() function is in there. We don't need it, though. We can stop the loop at the square root of n using another int and without a multiply.

Code:

int cc;
for(c=2,cc=3;(cc+cc-c)<n;c++,cc+=c){
...
}

I have not benchmarked if these added int steps beat the sqrt() function being added in there. They definitely do on some architectures.

If we're talking about speed we can cut his number of loops in half by only checking odd numbers (like I did in my python source). No even number is prime since it is always divisible by two.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.