Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the number of distinct prime number of a given number, I am passing a array to the function and then multiplying all the elements of the array to generate the number, I am getting BUS ERROR for some test cases

What I have tried:

int Solution::solve(vector<int> &A) {
    long long int mul = 1;
     for(int i=0;(i<A.size()-1);i++)
     {
         mul = mul*A[i];
     }
    int new1 = mul;
    int arr[new1 + 1];
    memset(arr, 0, sizeof(arr));
    for (int i = 2; i * i <= new1; i++) 
    {
        if (!arr[i])
            {
                for (int j = 2 * i; j <= new1; j += i)
                {
                    arr[j]++;
                }
            }
 
        arr[i] = 1; 
    }
    return *max_element(arr, arr+new1);
}
Posted
Updated 5-Aug-18 4:27am
Comments
Eric Lynch 5-Aug-18 9:31am    
I'd add some exception handling, to find out which cases cause the error, and then step through the debugger, for one of those cases, to find out which line causes the error.
Richard MacCutchan 5-Aug-18 9:36am    
"I am getting BUS ERROR for some test cases"
What test cases, and where do you get the error?
CPallini 6-Aug-18 2:52am    
1. I want to find the number of distinct prime number of a given number,
2. I am passing a array to the function and then multiplying all the elements of the array to generate the number,
How are sentences (1) and (2) related?
KarstenK 6-Aug-18 4:03am    
Really strange code, doesnt work for prime nummber.
Write some test data, include more output and use the debugger.

1 solution

This code is weird:
C++
for(int i=0;(i<A.size()-1);i++)
{
    mul = mul*A[i];
}

because it multiply all number of the vector but the last one.
Quote:
I want to find the number of distinct prime number of a given number

Can you define this ?
Because as I understand it, it is not what is doing your code, it- is not even close.
As I understand it, your code return a list of all prime numbers below mul.

Complete your code so it can be run.
Show an input example that give you the error message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900