Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQrelated cookies.

Introduction to Linux  A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free. 


09022012, 01:10 PM

#1

Member
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Rep:

C fibonacci and prime
In C
A Program That takes a number and checks whether it is prime and also belongs to fibonacci series



09022012, 01:46 PM

#2

Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,506

Is there a question in there?
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.
Last edited by sag47; 09022012 at 04:08 PM.



09022012, 02:10 PM

#3

Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928

It's probably word for word from the homework assignment.
How about you write the code and see if it runs, if it doesn't then post it and we'll see what is wrong.



09022012, 03:47 PM

#4

Member
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Original Poster
Rep:

My code like this tell me changes
#include<stdio.h>
main()
{
int n, c = 2,k,result;
printf("Enter a number to check if it is prime and fibonacci\n");
scanf("%d",&n);
for ( c = 2 ; c <= n  1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
k=n;
;
fibonacci(k);
return 0;
}
int fibonacci(int n)
{
int a = 0;
int b = 1;
int sum;
int i;
while(sum<n)
{
sum = a + b;
a = b;
b = sum;
if (sum==n)
printf("%d",sum);
}
return 0;
}



09022012, 04:06 PM

#5

Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632

Use [CODE][/CODE] tags to format your code.
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.
Last edited by Snark1994; 09022012 at 04:08 PM.



09022012, 04:06 PM

#6

LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Quote:
My code like this tell me changes

please?
Have you tested it?



09022012, 04:16 PM

#7

Member
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Original Poster
Rep:

thanks for wonderful replies got it working now



09022012, 05:06 PM

#8

Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,506

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
Last edited by sag47; 09022012 at 05:14 PM.



09022012, 11:39 PM

#9

Member
Registered: Jul 2012
Location: Pune, India
Distribution: Mageia 4
Posts: 183
Rep:

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++){
...
}



09032012, 01:39 AM

#10

Senior Member
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Rep:

Quote:
Originally Posted by kedarp
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+ccc)<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.



09032012, 02:08 AM

#11

Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,506

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.



09032012, 01:15 PM

#12

Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632

Balvinder87, can you mark this thread as 'SOLVED' please? Thanks.
And if we're going to do it in other languages, then why not haskell:
Code:
isPrime n = n > 1 && foldr (\p yes > p*p > n  ((n `rem` p) /= 0 && yes)) True primes
primes = 2 : filter isPrime [3,5..]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
primeFib x = (x `elem` (takeWhile (<=x) fibs)) && (isPrime x)
(Credit to HaskellWiki for an 'isPrime' oneliner)



Thread Tools 
Search this Thread 


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT 5. The time now is 09:01 AM.

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

Latest Threads
LQ News

