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;
}
int __fastcall TForm1::Factorial(int x)
{
int i,value;
value=1;
for(i=1;i<x+1;i++)
value*=i;
return value;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StringGrid1->Cells[0][0]="First digits";
StringGrid2->Cells[0][0]="Not first";
StringGrid1->Cells[1][0]="Factorial";
StringGrid2->Cells[1][0]="Factorial";
StringGrid1->Cells[2][0]="First d. in F.";
StringGrid2->Cells[2][0]="First d. in F.";
StringGrid1->Cells[3][0]="Not f. in Fa.";
StringGrid2->Cells[3][0]="Not f. in Fa.";
StringGrid1->Cells[4][0]="First/not_first";
StringGrid2->Cells[4][0]="First/not_first";
StringGrid1->Cells[5][0]="Value:";
StringGrid2->Cells[5][0]="Value:";
int first[4],not_first[4],factorial_first[4],factorial_not_first[4];
int first_el[4],first_el2[4],not_first_el[4],not_first_el2[4];
float div_first[4],div_first2[4];
float new_value[4],new_value2[4];
int i,j;
i=0;j=2;
do
{
if(is_First(j))
{
first[i]=j;
++i;
}
++j;
}while(i<4);
i=0;j=1;
do
{
if(!is_First(j))
{
not_first[i]=j;
++i;
}
++j;
}while(i<4);
for(i=0;i<4;i++)
{
factorial_first[i]=Factorial(first[i]);
factorial_not_first[i]=Factorial(not_first[i]);
first_el[i]=0;
first_el2[i]=0;
not_first_el[i]=0;
not_first_el2[i]=0;
}
unsigned k;
i=0;
do
{
for(k=1;k<factorial_first[i]+1;k++)
{
if(is_First(k))
first_el[i]++;
else
not_first_el[i]++;
}
++i;
}while(i<4);
i=0;
do
{
for(k=1;k<factorial_not_first[i]+1;k++)
{
if(is_First(k))
first_el2[i]++;
else
not_first_el2[i]++;
}
++i;
}while(i<4);
for(i=0;i<4;i++)
{
div_first[i]=(1.*first_el[i])/(1.*not_first_el[i]);
div_first2[i]=(1.*first_el2[i])/(1.*not_first_el2[i]);
new_value[i]=div_first[i]*factorial_first[i];
new_value2[i]=div_first2[i]*factorial_not_first[i];
}
for(i=0;i<4;i++)
{
StringGrid1->Cells[0][i+1]=IntToStr(first[i]);
StringGrid2->Cells[0][i+1]=IntToStr(not_first[i]);
StringGrid1->Cells[1][i+1]=IntToStr(factorial_first[i]);
StringGrid2->Cells[1][i+1]=IntToStr(factorial_not_first[i]);
StringGrid1->Cells[2][i+1]=IntToStr(first_el[i]);
StringGrid2->Cells[2][i+1]=IntToStr(first_el2[i]);
StringGrid1->Cells[3][i+1]=IntToStr(not_first_el[i]);
StringGrid2->Cells[3][i+1]=IntToStr(not_first_el2[i]);
StringGrid1->Cells[4][i+1]=FloatToStr(div_first[i]);
StringGrid2->Cells[4][i+1]=FloatToStr(div_first2[i]);
StringGrid1->Cells[5][i+1]=FloatToStr(new_value[i]);
StringGrid2->Cells[5][i+1]=FloatToStr(new_value2[i]);
}
}
Brak komentarzy:
Prześlij komentarz