Topic: Generate bilangan prima dengan algoritma Sieve of Eratosthenes

Bilangan prima adalah bilangan (>1) yang hanya habis dibagi oleh bilangan 1 dan bilangan itu sendiri, yakni: 2, 3, 5, 7, 11, 13, 17, ....

Untuk generate bilangan prima secara cepat, dapat dilakukan dengan menggunakan algo sieve of Eratosthenes, algo Eratosthenes ini cepet karena dia mengenerate bilangan prima dengan sebelumnya mencoret bilangan-bilangan yang bukan prima dan kelipatannya.

PunBB bbcode test

Untuk ilustrasi lebih mendatail mengenai cara kerjanya dapat dilihat lebih lengkap di http://en.wikipedia.org/wiki/Sieve_of_eratosthenes

Dibawah ini code generate bilangan prima yang < 10000000 dengan Sieve of Eratosthenes yang telah dimodifikasi, supaya lebih cepat.
Cara mempercepat:
- Coret semua yang habis dibagi 2 dengan operasi modulo
- Cek hanya untuk setengah dari kuadrat jumlah bilangan (jika akan mengenerate 10000 bilangan prima, tidak melakukan pemeriksaan dari 1-10000, tapi hanya dari 1-100 saja, karena jika bilangan awalnya bukan prima, maka kuadratnya juga pasti bukan prima)

/* Sieve of Eratosthenes + pruning++ , 3 times faster than Sieve of Erastosthenes*/
/* Displaying prime numbers lower than 10000000 */
#define max1 10000000

long i,n,a,c,j; bool flag[max1];

int main()
    time_t begin = clock();
    c = sqrt(max1);
    flag[2] = true;
    for(i=3; i<=max1; i++) if(i%2==0) flag[i] = false; else flag[i] = true;
    for(i=2; i<=c; i++)
            for(j=i*i; j<max1; j+=i*2)
                flag[j] = false;
    for(i=2; i<=max1; i++)
        if(flag[i]==true) printf ("%ld\n", i);

    printf("%lf secs", double(clock()-begin) / CLOCKS_PER_SEC);

    return 0;

Last edited by StevenLuck (2009-07-22 16:23:46)


Re: Generate bilangan prima dengan algoritma Sieve of Eratosthenes

replicawatches replicawatches  produced. replica panerai watches replica panerai watches  For that cause gmt 2 rolex , replica rolex watches for sale replica rolex watches for sale  they may have no must invest a considerable sum of money on just an authentic Rolex timepiece womens rolex watches . lady rolex watches rolex 116519  Considering that reproduction Rolex look at can meet up with our desire in schedule daily life and is available in less costly price tag at the same time viewing as superior high quality mens fashion clothing , the important thing explanation why don't you purchase the vast majority of these replicas? The rolex replicas in latest industry seem and features much like the correct true types. They can be available in various internet websites to satisfy the precise wants of all individuals who are anticipating unique Rolex watches even so constrained by their constrained lender accounts. Higher high quality Rolex imitations


Re: Generate bilangan prima dengan algoritma Sieve of Eratosthenes

Couture Club to host an Internet forum, and Forbes as keynote at the JA Show  | The Couture Diamond Leadership Conference will host a luncheon discussion by Internet experts at the Couture Club at JA New York on Monday  July 26  from 12:30 to 2 p.m.  James Hurley  associate director of Bear Stearns &amp; Co.  will present research findings from the company’s in-depth analysis of the new jewelry e-tailing world. Following patek philippe grand complications replica   Ann Poletti  director  Jewelry &amp; Watches  eBay  Inc.; Eric Broussard  Amazon.com vice president of Jewelry; and Ofer Azrielant  chairman/CEO of Jewelry.com will discuss Internet issues in an informal  talk show format.  Following the Internet luncheon  the Couture Club will offer members the expertise of Temel Inc.  an integrated marketing communications company and a pioneer in providing  will conduct an Internet Workshop from 2 to 3 PM on branding strategies for the Internet  usability “best practices ” and ways to reach a targeted audience through branded multi-channel communications. A white paper will be available for all Couture Club participants.  From 3 p.m. to 5 p.m. replica watches paypal   Couture retailers can sign up for free one-on-one 15-minute Web site Strategy Sessions with a Temel representative. Sign-up is on a first come first served basis.  On  July 25 tissot prs200 replica watches   at 12:30 p.m.  Couture Club will present its keynote/luncheon speaker chronoswiss watch  replica cheap breitling watches   Steve Forbes  president/CEO of Forbes  Inc. and editor-in-chief of Forbes magazine  who will provide his economic forecast with a focus on the effect of the 2004 elections on the luxury market.  The Couture Club is a private networking and meeting venue accessible for Couture retailers and exhibitors at the summer and winter JA New York Shows by invitation-only. Produced by Couture Jewellery Collection &amp; Conference in association with JANY and VNU Expositions  Couture Club provides complimentary cappuccino  lunch  refreshments  cocktails  concierge  business  and meeting facilities. For information  call 646-654-7669.


Re: Generate bilangan prima dengan algoritma Sieve of Eratosthenes

Thank you for good communication.