Recursive prime number Enhancement edition

Source: Internet
Author: User

Once wrote a code about recursive primes, today I think of the code can also be optimized, speed and the length of the code slightly optimized some
/*primes recursive with colder C-version at Http://winxos.blog.51cto.com/1671107/652371winxos 2015-9-19*/#include < Stdio.h>int isdiv (int a,int b) {    if (b*b>a) return 0;    if (a%b==0) return 1;    Isdiv (a,b+1);} int isprime (int n) {    return n>=2 &&!isdiv (n,2);} int main () {    int i;    for (i=1;i<10000;i++)    {        if (IsPrime (i) ==1)        {            printf ("%d", I);        }    }    return 0;}
Because the C language does not support the default parameter passing, leading to the number of recursive methods need to be divided into two functions to complete, otherwise it will be passed two parameters in order to call up with the traditional prime number of the decision function is different, but also not good to see, so I have designed a C + + version, The code is more concise and the use of the same as the traditional function, wrote two versions, open O2 situation fast version of about one times, because each add 2
/*primes recursive with CPP Versionolder C version @ http://winxos.blog.51cto.com/1671107/652371c can not use default par Ameter, so can not use only one function with one Parameterwinxos 2015-9-19*/#include <iostream> #include <CTIME&G T;using namespace Std;//fast versionint isprime_fast (int a,int b=3) {    if (a==2 | | a==3) return true;    if (a%2==0 | | a%b==0 | | a<2) return false;    if (b*b>a) return 1;    Isprime_fast (a,b+2);} More Pretty and Simpleint isprime (int a,int b=2) {    if (a>1 && b*b>a) return true;    if (a%b==0 | | b>a) return false;    IsPrime (a,b+1);} int main () {    int i,ct=0,st=clock ();    for (i=1;i<5000000;i++)    {        if (isprime_fast (i))        {        ct++;            cout<<i<< "";        }    }    cout<< "Total:" <<ct<< "used:" <<float (Clock ()-st)/clocks_per_sec<< "s." <<endl;    return 0;}


This article is from "Winxosのcoding World" blog, please make sure to keep this source http://winxos.blog.51cto.com/1671107/1696268

Recursive prime number Enhancement edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.