Prime Cuts

if you have any problem in anything related to our career, tell us and we will do our best

Prime Cuts

Postby lo2lo2a on Sat 04 Apr, 2009 6:06 pm

al salamo 3alaykom
27na 7alena l mas2ala de by Sieve bs gt run time error
l moshkela ba2a ana ma3rafsh ya3ny a run time error da ya3ny moshkelto feen?
+ 3'aleban kda fe 7aga ana msh fahmaha fel mas2ala ya3ny hwa haybda2 y3ml display mnen 3alshan fel UVa kateb haybda2 mn l center of the list 27na tawasalna 2no yo2sod l interval nafsaha 2ly hada5alha msh yo2sod l list bta3t l prime numbers ya3ny bs l moshkela fel display haybda2 mnen ya3ny?

this is the code:

Code: Select all
#include <iostream>
#include <cmath>
#define LIMIT 1001
using namespace std;
bool prime[LIMIT];
void Sieve()
{
int i, j;
  for(i=0; i<LIMIT; i++)
        prime[i] = 1;
  for(i=2; i<sqrtl(LIMIT) + 1; i++)
        if(prime[i])
        {
            for(j = i*i ; j < LIMIT ; j += i)
                    prime[j] = 0;
        }
}
int main ()
{
   Sieve();
   int Number,x,y , count;
   while (cin >> Number >> x)
     {
       count = 0;
       cout<<Number<<" "<<x<<": ";
       if(x != Number)
       {
         if(Number % 2 == 1)
         {
          y = (x*2)-1;
          for(int i = y+1 ;  ; i++)
          {
            if(count == y)
               break;
            if(prime[i])
             {
                cout<<i<<" ";
                count++;
             }
          }
          cout<<endl;
          }
          else
          {
            y = (x*2);
            for(int i = y-1 ;  ; i++)
            {
               if(count == y )
                  break;
               if(prime[i])
                {
                   cout<<i<<" ";
                   count ++;
                }
               
            }
            cout<<endl;

          }
       }
       else
       {
       for(int i = 1 ; i < Number ; i++)
          {
            if(prime[i])
               cout<<i<<" ";
          }
       cout<<endl;
       }      
       cout<<endl;
      }
   return 0;
}


thnx for ur help in advance :D
lo2lo2a
 
Posts: 7
Joined: Mon 29 Dec, 2008 4:33 pm

Re: Prime Cuts

Postby Jaqoup on Tue 07 Apr, 2009 11:16 pm

howwa mabda2eyan ana mesh faker el mas2ala :D
bass runtime error ya3ny el program bey-crash fel nos
e.g. array out of bounds accessing aw division by zero ....
User avatar
Jaqoup
 
Posts: 53
Joined: Sun 07 Dec, 2008 8:46 am
Location: Alexandria

Re: Prime Cuts

Postby amrsaqr on Tue 07 Apr, 2009 11:53 pm

Hashoof el code delwa2te ya hadeel, give me some time bas.
amrsaqr
 
Posts: 19
Joined: Mon 08 Dec, 2008 4:59 pm

Re: Prime Cuts

Postby amrsaqr on Wed 08 Apr, 2009 1:06 am

garaby el case de ya hadeel
10 9
el mafrood tetala3 1 2 3 5 7
el code bta3ek bytala3 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
ta2reban keda sabab el 3'alta de
awwalan 2ennek mesh mzabata el 7esba bta3et el center sa7 (el equation bta3etha ya3ne)
w kaman feeh el loop de for(int i = y-1 ; ; i++) 3amla 7aga 3'alat ta2reban
7awle tsala7y ba2a

w feeh shweyt notes keda
fel problem statement by2ollek koll raqam fel list byb2a preceded by one space, ya3ne ableeh mesh ba3deeh, enty 3amla el space ba3d el raqam, fa da law el code sa7 kan hayddeky presentation error
w mola7za tanya bardo fel function bta3et sieve
ennek 3amla fel loop keda i<sqrtl(LIMIT) + 1
w function sqrtl de function bate2a
fante momken te7sebeha marra wa7da abl el loop f variable w t7ote el variable fel condition
3ashan mayo3odsh koll amma yegy y-check fel condition y5osh gowwa el sqrtl de w y7seb, la2enaha function bate2a zay ma2oltelek

bas keda, atmanna 2enne akoon sa3adt
amrsaqr
 
Posts: 19
Joined: Mon 08 Dec, 2008 4:59 pm

Re: Prime Cuts

Postby lo2lo2a on Wed 08 Apr, 2009 10:41 pm

amrsaqr wrote:garaby el case de ya hadeel
10 9
el mafrood tetala3 1 2 3 5 7
el code bta3ek bytala3 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
ta2reban keda sabab el 3'alta de
awwalan 2ennek mesh mzabata el 7esba bta3et el center sa7 (el equation bta3etha ya3ne)
w kaman feeh el loop de for(int i = y-1 ; ; i++) 3amla 7aga 3'alat ta2reban
7awle tsala7y ba2a

w feeh shweyt notes keda
fel problem statement by2ollek koll raqam fel list byb2a preceded by one space, ya3ne ableeh mesh ba3deeh, enty 3amla el space ba3d el raqam, fa da law el code sa7 kan hayddeky presentation error
w mola7za tanya bardo fel function bta3et sieve
ennek 3amla fel loop keda i<sqrtl(LIMIT) + 1
w function sqrtl de function bate2a
fante momken te7sebeha marra wa7da abl el loop f variable w t7ote el variable fel condition
3ashan mayo3odsh koll amma yegy y-check fel condition y5osh gowwa el sqrtl de w y7seb, la2enaha function bate2a zay ma2oltelek

bas keda, atmanna 2enne akoon sa3adt

mal2etsh zorar l thnx 2olt aktebo ana ba2a w 5alas
isA awel ma a5las mn yasser wel exams hazabatha
thnx for ur help keteeeeeeeeeeer :D
lo2lo2a
 
Posts: 7
Joined: Mon 29 Dec, 2008 4:33 pm


Return to General Mentor

Who is online

Users browsing this forum: No registered users and 1 guest

cron