Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] C fibonacci and prime
 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

 09-02-2012, 12:10 PM #1 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 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
 09-02-2012, 12:46 PM #2 sag47 Senior Member   Registered: Sep 2009 Location: Orange County, CA Distribution: Kubuntu x64, Raspbian, CentOS Posts: 1,848 Blog Entries: 36 Rep: 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; 09-02-2012 at 03:08 PM.
 09-02-2012, 01:10 PM #3 H_TeXMeX_H LQ Guru   Registered: Oct 2005 Location: \$RANDOM Distribution: slackware64 Posts: 12,928 Blog Entries: 2 Rep: 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.
 09-02-2012, 02:47 PM #4 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: My code like this tell me changes #include 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
 09-02-2012, 03:06 PM #5 Snark1994 Senior Member   Registered: Sep 2010 Location: Wales, UK Distribution: Arch Posts: 1,632 Blog Entries: 3 Rep: 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 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; 09-02-2012 at 03:08 PM.
09-02-2012, 03:06 PM   #6
pixellany
LQ Veteran

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

Rep:
Quote:
 My code like this tell me changes

Have you tested it?

 09-02-2012, 03:16 PM #7 Balvinder87 Member   Registered: Jun 2012 Location: India Distribution: debian Posts: 77 Blog Entries: 1 Original Poster Rep: thanks for wonderful replies got it working now
 09-02-2012, 04:06 PM #8 sag47 Senior Member   Registered: Sep 2009 Location: Orange County, CA Distribution: Kubuntu x64, Raspbian, CentOS Posts: 1,848 Blog Entries: 36 Rep: 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; 09-02-2012 at 04:14 PM.
 09-02-2012, 10:39 PM #9 kedarp Member   Registered: Jul 2012 Distribution: Ubuntu Posts: 197 Blog Entries: 3 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
09-03-2012, 12:39 AM   #10
Skaperen
Senior Member

Registered: May 2009
Location: WV, USA
Distribution: Slackware, Ubuntu, Amazon Linux
Posts: 1,830
Blog Entries: 21

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
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.

 09-03-2012, 01:08 AM #11 sag47 Senior Member   Registered: Sep 2009 Location: Orange County, CA Distribution: Kubuntu x64, Raspbian, CentOS Posts: 1,848 Blog Entries: 36 Rep: 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.
 09-03-2012, 12:15 PM #12 Snark1994 Senior Member   Registered: Sep 2010 Location: Wales, UK Distribution: Arch Posts: 1,632 Blog Entries: 3 Rep: 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' one-liner)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post explorer *BSD 6 05-27-2009 05:20 PM muskvar Programming 2 09-26-2007 08:16 AM BrokenFighter Programming 10 03-10-2007 09:41 PM debiant Programming 13 09-09-2006 11:10 PM WorldBuilder Programming 5 12-17-2003 01:41 AM

All times are GMT -5. The time now is 04:40 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -