Hi guys, I am doing a question which is as follow:
Using this small set of five numbers: 2, 3, 4, 5, 6; there are 10 pairs that can be formed: (2,3), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6), (4,5), (4,6) and (5,6). Six of these 10 pairs: (2,3), (2,5), (3,4), (3,5), (4,5), and (5,6) have no common factor other than one. Using the ratio of the counts as the probability we have:
6/p2 = 6/10. Hence the estimated value of p is 3.1623, correct to four decimal places.
As another example, given this set of 10 numbers {32391, 14604, 3902, 153, 292, 12382, 17421, 18716, 19718, 19895}, there are 24 pairs that have no common factor other than one, among a total of 45 pairs. We have:
6/p2 = 24/45. Hence the estimated value of p is 3.3541, correct to four decimal places.
I keep encountering the Arithmetic Exception (Core Dumped) problem. Could any kind soul please give me some advice? Thanks! Below is my programme:
Code:
#include <stdio.h>
#include <math.h>
#define N 50
double estimate(int [], int);
int main(void)
{
int arr[N],i,size;
double pi;
printf("Enter the size of the array:\n");
scanf("%d", &size);
for(i=0;i<size;i++)
{
scanf("%d", &arr[i]);
}
pi = estimate(arr, size);
printf("%lf", pi);
return 0;
}
// Write a description of the function here
double estimate(int arr[], int size)
{
int i,j,k,count,total_count=0,found=0;
double pi;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
total_count++;
count = 0;
if(arr[i] > arr[j])
{
for(k=2;k<=arr[j];k++)
{
if(arr[i] % k == 0 && arr[j] % k == 0)
count++;
}
}
else
{
for(k=2;k<=arr[i];k++)
{
if(arr[j] % k == 0 && arr[i] % k ==0)
count++;
}
}
if(count==1)
found++;
}
}
pi = sqrt(6 / (found/total_count));
return pi;
}
Thanks for your help!