SIGABRT error for - https://www.codechef.com/problems/PRIME1 - c++

I am using sieve of eratosthenes to solve this problem but it is giving me SIGABRT error although my code is working fine on codeblocks....
Please help me modify this code to remove error....
My code is...
#include<vector>
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
unsigned long int t, n, m,i,j;
vector<int> prime;
cin>>t;
while(t--)
{
cin>>m;
cin>>n;
while(!(1<=m&&m<=n&&n<=1000000000&&n-m<=100000))
cin>>m>>n;
prime.resize(n);
for(i=0;i<n;i++)
prime[i]=1;
prime[0]=0;
prime[1]=0;
for(i=2;i<sqrt(n);i++)
{
if(prime[i]==1)
{
for(j=i;i*j<=n;j++)
prime[i*j]=0;
}
}
for(i=m;i<=n;i++)
{
if(prime[i]==1)
cout<<i<<endl;
}
cout<<endl;
prime.resize(0);
}
return 0;
}

Your j loop allows i*j to equal n, but the vector of size n must be indexed from 0 to n-1. The existing code permits referencing an element out of bounds.
The same problem can occur in the last loop, too.

The SIGABRT is issued by library routines.
You have two library routines in your program: std::vector and sqrt.
Either assign sqrt(n) to a const variable or replace the condition with:
(i * i) < n;
You need to verify that:
prime[i*j]
is a valid location. In other words, (i * j) < n.
Some information about primes (that can help you code your program):
Prime numbers are odd except the value 2.
Your test value can start at 3 and add 2 to get to the next value.
You may be able to save some time by looking values in an array of
known values.
Multiplication is usually faster than division. Try rewriting your
test to use multiplication and not division.
Use a data type that can contain the maximum value.

Related

Modulo strength , want explanation in the algorithm used to compute the answer

I was trying to solve the problem Modulo strength at hackerearth ,
https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/golf/modulo-strength-4/ , so basically we have to find all such pairs of no. (say i,j) such that A[i]%k=A[j]%k where k is a no. given in the question , i tried brute force approach and got time limit exceeded at some of the last test cases and in the discussion tab i found a code which is working but i couldn't understand what exactly it does, and the underlying thinking behind the algorithm used.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int n,k,s=0;
cin>>n>>k;
int a[n];
vector<int>v(k,0); // Specially this part ,what does it store?
for(int i=0;i<n;i++)
{
cin>>a[i];
v[a[i]%k]++;
}
for(int i=0;i<k;i++)
{
s+=v[i]*(v[i]-1);
}
cout<<s;
}
Here is the code, i wanted to understand it so that i can apply this logic over other problems.
There are a few problems with that;
"bits/stdc++.h" is not a standard header
Variable-length arrays, like int a[n], are non-standard and prone to runtime errors (this one is also completely unnecessary)
#define int long long makes the code have undefined behaviour.
Here is a fixed version, with some minor renaming and clarifying comments:
#include <iostream>
#include <vector>
int main() {
long long n, k;
cin >> n >> k;
// There are k groups of friends.
std::vector<int> friends(k);
// Count how many people there are in each group.
for(int i = 0; i < n; i++)
{
int x;
std::cin >> x;
friends[x%k]++;
}
long long sum = 0;
for(int i = 0; i < k; i++)
{
// In a group of N mutual friends, each person has N-1 friends.
sum += friends[i] * (friends[i]-1);
}
std::cout << sum;
}
Let's first go through with the purpose of every variable in the code.
The purpose of n,k,s is explicitly given.
a[n] is for reading the numbers in array.
std::vector<int>v(k,0) stores k sized vector of 0's, and v[i] indicates the number of variables in a[n] for which a[j]%k==i.
In the last loop, the following has done. The number of pairs that can be constructed with n elements is n*(n-1) (basic combinatorics), and if we have v[i] numbers for which the condition is satisfied and a[j]%k==i the number of pairs that can be constructed is v[i]*(v[i]-1). The loop sums up the number of pairs for every remnant i.

Segmentation fault for higher values of n(e.g n=999997)

I am using dynamic programming for a problem where I will be given n and need to output minimum number of operations to get 1 from n by using these three operations( -1 ,/2 ,/3). Given constraint(1<=n<=10^6)
My code is giving segmentation fault(somewhere in solve function) for higher values of n(e.g 977775,1000000) but running fine of smaller input(e.g 100000).
I have tried a lot but I am unable to find problem in my code. Also is there any other way I can apply dynamic programming in this question.
Any help would be appreciated.
#include<iostream>
using namespace std;
int solve(int n,int a[])
{
if(n==1)
return 0;
if(a[n]!=-1)
return a[n];
int ans=100000;
for(int i=0;i<3;i++)
{
if(i==0)
{
a[n]=solve(n-1,a)+1;
if(a[n]<ans)
ans=a[n];
}
else if(i==1 && n%2==0)
{
a[n]=solve(n/2,a)+1;
if(a[n]<ans)
ans=a[n];
}
else if(i==2 && n%3==0)
{
a[n]=solve(n/3,a)+1;
if(a[n]<ans)
ans=a[n];
}
}
return ans;
}
int main()
{
int n;
cin>>n;
int a[n+1];
for(int i=0;i<=n;i++)
a[i]=-1;
int ans=solve(n,a);
cout<<ans<<endl;
return 0;
}
The problem is that you can only store up to 8 digits in an int variable in C++, I would switch from int ans to double ans to fix the problem, also keep in mind that double can store up to 34 digits, if you try to do store more digits than the maximum allowed number of digits, weird things happen, and in that case you get a stackOverflow..... Also consider the following: In your function, you are calling about 999997 recusive calls * 3 which is about 3000000 recusive calls for the stack, which is going to use way too much memory and hence Segmentation fault.....

I am working on finding the min value in a random array, and anyone know that why my code is not working? C++

Here's what I did.
I am working on find the min value in a random array,
Anyone know that why my code is not working?
It output with lowest valus : 0 and located on a complex number
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int x,min,n;
int array[1000];
for(int i=1;i<=1000;i++)
{
x = rand()% 1000 + 1;
array[i] = x;
}
min=array[0];
for(int i=0;i<1000;i++)
{
if(array[i]<min)
{
min=array[i];
n=i;
}
}
cout<<"The Lowest Value : "<<min<<"\nIts located at { "<< n <<" }"<<endl;
cout<<" "<<endl;
return 0;
}
Get used to always initialising your variables. In this case you use n before it is written first time.
int x=1,min=1001,n=0;
Also heed the hint by seccpur (alternatively mine) from comments.
Here is mine again, for making a complete answer:
Get used to for(int i=0;i<arraysize;i++) in C++. Otherwise you will always trip over the 0-indexing.
for(int i=0;i<1000;i++)
Both problems together cause your problem of outputting the weird index (from non-initialised n) and the implausible value 0 (from non-initialised array[0]).
With your way of random numbers, 0 is impossible, but if it happens to be in index 0, then it will be the lowest of all and never be replaced by anything meangingful.
You got "lucky" (actually I consider it unlucky not being told about this kind of error...) with accessing beyond the array...

Why does my program(code chef- two numbers) crash when i give higher values to variables

Question: https://www.codechef.com/problems/TWONMS.
My code:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
int t,a,b,n,c,d,FinalNumber;
cin>>t;
for(int i= 0;i<t;i++){
cin>>a;
cin>>b;
cin>>n;
c=a;
d=b;
int n1 = n/2;
int n2 = n - n1;
while(n1 != 0){
c=c*2;
n1--;
}
while(n2 != 0){
d=d*2;
n2--;
}
if(c>d){
FinalNumber = c/d;
cout<<FinalNumber<<endl;
}
else{
FinalNumber = d/c;
cout<<FinalNumber<<endl
}
}
}
This program crashes when i enter the value of N above 30,what can i do to make it handle bigger numbers?And also is my program correct?
Based on your code, n1 which is initially assigned n/2 is used as a counter for a doubling operation on c: c=c*2;.
Depending on the value of c, d. and n, there could certainly be overflow happening which may cause a range of problems.
Note that these coding exercises are not a good way to learn how to program well. Although they could be stimulating you in improving your skills, they often encourage users to write bad code quality.
In your code, c and d goes into integer overflow: your number is way too big to fit in a 32 bit integer number.
Websites like codechef expects you to think further: applying the brute force algorithm will only work on small inputs, you would have to find a better approach to solve the problem with the constrains that are given.

Error in program for modified bubble sort

Codeforces problem 339A-http://codeforces.com/problemset/problem/339/A
I have tried to sort the values stored at even places of the array(starting from 0).I get an error while running the program or program returns a junk value.What is wrong with my solution?
My solution:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[101],temp;
int i,j;
cin>>s;
for(i=0;i<strlen(s);i+=2) //Bubble sorting values at even values of i.'+' is stored at odd values of i.
{
for(j=0;j<(strlen(s)-i-2);j+=2)
{
if(s[j]>s[j+2])
{
temp=s[j];
s[j]=s[j+2];
s[j+2]=temp;
}
}
}
cout<<s;
}
Your compiler should have warned you about the problem (you did switch on all warnings, yes? always do that!): Once i==strlen(s)-1, the loop for j is essentially unbounded, by the magic of arithmetic rules for signed/unsigned values.
for(unsigned j=0; j+2+i < strlen(s); j+=2)
does not have this problem. (i should be unsigned as well.)
Or stop the loop for i earlier. The problem in your code is still there then, but you won’t run into it. But I believe that is the worse route to take – fix the bug, and then optimize by observing i doesn’t need to go as far up, because the last character already forms a sorted sequence.
For odd lengths len of s, the outer loop runs until i==len-1. The inner loop then terminates at len - len - 1 - 2. Since strlen returns an unsigned type, this evaluates to a very large unsigned number, causing the inner loop to read way beyond the end of s. Eventually you'll reach memory you don't have access to read or write, causing the crash.
You can fix this by ending the outer loop sooner
int len = strlen(s);
for(i=0;i<len-2;i+=2) {
for(j=0;j<(len-i-2);j+=2)
Change this:
for(i=0;i<strlen(s);i+=2)
Into this:
for(i=0;i<strlen(s) - 2;i+=2)
Otherwise the s value be handled beyond its end-point
here is my code
void bubble(int a[], int n){
for(int i=0; i<n; i++){
int swaps=0;
for(int j=0; j<n-i-1; j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
swaps++;
}
}
if(swaps==0)
break;
}
}