piątek, 17 lutego 2017

Prime numbers (C++ Builder)




private: // User declarations
bool __fastcall is_First(int x);
void __fastcall init_Table();

public: // User declarations
int *my_first;
int *difference;
int *how_many;
int *how_many2;
int *not_first_minus;
int *not_first_minus2;
AnsiString text[50];
AnsiString text2[50];


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
  my_first=new int[50];
  difference=new int[50];
  how_many=new int[50];
  not_first_minus=new int[50];
  how_many2=new int[50];
  not_first_minus2=new int[50];
}
//---------------------------------------------------------------------------
 bool __fastcall TForm1::is_First(int x)
 {
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
 if(x%i==0)
  ++sum;
if(sum==2)
 return true;
else
 return false;
 }
 void __fastcall TForm1::init_Table()
 {
int i,j;
i=0;
j=2;
do
{
 if(is_First(j))
 {
 my_first[i]=j;
 ++i;
 }
 ++j;

}while(i<50);
difference[0]=0;

for(i=1;i<50;i++)
{
difference[i]=my_first[i]-my_first[i-1];
}


for(i=0;i<50;i++)
{
 how_many[i]=0;
 how_many2[i]=0;
}
i=0;
do
{
 for(j=1;j<my_first[i]+1;j++)
  if(is_First(j))
++how_many[i];
  else
++how_many2[i];
  not_first_minus[i]=how_many2[i]-how_many[i];
  if(is_First(not_first_minus[i]))
text[i]="yes";
  else
text[i]="no";
  not_first_minus2[i]=(how_many[i]-(how_many2[i]+difference[i]))*-1;
  if(is_First(not_first_minus2[i]))
text2[i]="yes";
  else
text2[i]="no";


  ++i;

}while(i<50);

 }




void __fastcall TForm1::Button2Click(TObject *Sender)
{
 delete my_first;
 delete how_many;
 delete not_first_minus;
 delete not_first_minus2;
 delete how_many2;
 delete difference;
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Table();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int i;
 for(i=0;i<50;i++)
 {
ListBox1->Items->Add(IntToStr(my_first[i]));
ListBox2->Items->Add(IntToStr(difference[i]));
ListBox3->Items->Add(IntToStr(how_many[i])+" - " +IntToStr(how_many2[i]));
ListBox4->Items->Add(IntToStr(not_first_minus[i]));
ListBox5->Items->Add(text[i]);
ListBox6->Items->Add(IntToStr(not_first_minus2[i]));
ListBox7->Items->Add(text2[i]);
if((is_First(not_first_minus[i])) && is_First(not_first_minus2[i]))
 ListBox8->Items->Add(IntToStr(my_first[i]));
if((!is_First(not_first_minus[i])) && !is_First(not_first_minus2[i]))
 ListBox9->Items->Add(IntToStr(my_first[i]));
 }
}

Brak komentarzy:

Prześlij komentarz