Getting "Floating point exception: 8" (I'm using Visual Studio Code) - c++

#include <iostream>
using namespace std;
int findPrime(unsigned long long int number);
bool isPrime(unsigned long long int number);
int main(){
const unsigned long long int bound = 600851475143;
cout << findPrime(bound) << endl;
}
int findPrime(unsigned long long int number){
int largestFactor = 0;
for(int i = 1; i <= sqrt(number); i++){
if(number % i == 0){
int possible = number/i;
if(isPrime(possible))
largestFactor = possible;
}
}
return largestFactor;
}
bool isPrime(unsigned long long int number){
for(int i = 0; i <= sqrt(number); i++){
if(number % i == 0){
return true;
}
}
return false;
}
The purpose of this code is to find the largest prime factor. I am getting Floating point exception: 8 when running it, it is in Visual Studio Code and I am running from terminal.

In the first iteration of this code
bool isPrime(unsigned long long int number){
for(int i = 0; i <= sqrt(number); i++){
if(number % i == 0){
return true;
}
}
return false;
}
Division by zero is done, but it is not allowed. The loop should start from 2, not 0, for primality checking.

#include <iostream>
#include <math.h>
using namespace std;
int findPrime(long long int number);
bool isPrime(long long int number);
int main(){
long long int bound = 600851475143;
cout << findPrime(bound) << endl;
}
int findPrime(long long int number){
long long int largestFactor = 0;
for(int i = 1; i*i <=number; i++){
if(number % i == 0){
long long int possible = number/i;
if(isPrime(possible)){
largestFactor = possible;
largestFactor = 1;
}
}
}
return number;
if(largestFactor == 0){
return number;
}
return largestFactor;
}
bool isPrime(long long int number){
for(int i = 2; i*i <= number; i++){
if(number % i == 0){
return false;
}
}
return true;
}
I ran this instead and I'm getting still getting floating point exception: 8, which is really weird. I changed to unsigned long long int but then the number became stored as a negative number -443946297.

Related

Getting Bad_alloc thrown when running this code

I am getting a bad_alloc thrown when I run the executable generated from this code. I do not know why it's being thrown (I can guess it's coming from that new statement)
#include <iostream>
#include <chrono>
#include <cmath>
using namespace std;
void gen_numbers(long int numbers[], long int how_many)
{
for (int i = 0; i < how_many; i++)
{
numbers[i] = i;
}
}
bool is_prime(long int n)
{
if(n < 2)
return false;
for(int i=2; i<=sqrt(n); i++)
{
if(n % i == 0)
return false;
}
return true;
}
int count_prime_serial(long int numbers[], long int how_many)
{
int count = 0;
for(int i=0; i<how_many; i++)
{
if(is_prime(numbers[i]))
count++;
}
return count;
}
int main(){
long int n = 1000000000;
long int* numbers = new long int[n];
gen_numbers(numbers, n);
auto start = chrono::steady_clock::now();
int count = count_prime_serial(numbers, n);
auto end = chrono::steady_clock::now();
double compute_time = chrono::duration<double>(end - start).count();
cout<<"Total number of primes = "<<count<<endl;
cout<<"Total computation time = "<<compute_time<<endl;
return 0;
}
Is there any solution to stop getting this error during runtime?
I've tried compiling and running the exact same code on another machine and it ran just fine, this is why I am confused.

We have to maximize the modulo function

In this question we have to maximize the modulo function. A string is given And we have remove the element and check after removing a element which number give the maximum modulo answer.
Link:- https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/maximize-modulo-2-0cb15ded/
My code is not able to pass all test case it shows runtime error or wrong answer in most test case.
#include "bits/stdc++.h"
using namespace std;
int countDigit(long long n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int m,k;
cin>>m>>k;
string s;
cin>>s;
int ans = INT_MIN;
int n = countDigit(k);
if(n == m && stoi(s) < k){
cout<<stoi(s)<<endl;
continue;
}
else{
for(int i=0;i<m;i++){
string a = "";
for(int j=0;j<m;j++){
if(j == i){
continue;
}
else{
a += s[j];
}
}
int mo;
int num = stoi(a);
mo = num%k;
if(mo > ans){
ans = mo;
}
}
cout<<ans<<endl;
}
}
return 0;
}
Please tell me where i'm making the mistake or please tell me any better way to solve this question?

Princess Farida on Spoj

I stuck at a problem SPOJ.
I checked all the test cases passing all of them, but I am still getting "WA" on spoj.
I know it can be solved using dynamic programming, but I am practicing memoization.
Here is my code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector <int> dp(1000000);
long long int maxloot(vector<int> &loot, int n) {
if (n == 0)
return 0;
if (n == 1)
return loot[0];
if (n == 2)
return max(loot[0], loot[1]);
if (dp[n] != -1)
return dp[n];
long long int take = loot[n - 1] + maxloot(loot, n - 2);
long long int leave = maxloot(loot, n - 1);
return dp[n]= max(take, leave);
}
int main() {
int t;
cin >> t;
int p = 1;
while (t--) {
int n;
cin >> n;
vector <int> loot;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
loot.push_back(temp);
}
dp.assign(1000000, -1);
cout <<"Case "<<p<<": "<< maxloot(loot, n)<<endl;
p++;
dp.clear();
}
}
Test case 1:
5
1 2 3 4 5
Test case 2:
1
10
output 1:
9
output 2:
10
You are using wrong data type to store value in vector dp.
As the sum of coins can go up to (10^9*10^2=10^11) therefore int would not be able to store it .Try using long long int instead as it would not lead to overflow condition.
SAME CODE AS YOURS(using long long int got accepted):
USE: vector< long long int>dp(1000000)
ACCEPTED CODE:
#include<iostream>
#include<vector>
#include<algorithm>
#define ull unsigned long long
using namespace std;
vector <long long int> dp(1000000);
long long int maxloot(vector<int> &loot, int n) {
if (n == 0)
return 0;
if (n == 1)
return loot[0];
if (n == 2)
return max(loot[0], loot[1]);
if (dp[n] != -1)
return dp[n];
long long int take = loot[n - 1] + maxloot(loot, n - 2);
long long int leave = maxloot(loot, n - 1);
return dp[n]= max(take, leave);
}
int main() {
int t;
cin >> t;
int p = 1;
while (t--) {
int n;
cin >> n;
vector <int> loot;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
loot.push_back(temp);
}
dp.assign(1000000, -1);
cout <<"Case "<<p<<": "<< maxloot(loot, n)<<endl;
p++;
dp.clear();
}
}

Runtime error in code (C++)

i am a beginner to c++ but i wouldn't have asked this question if i didnt spend hours on it.
The code is about finding primes between two numbers in the most efficient way where maximum limit is 10^9.
The following code gives me runtime error but i have no idea why.. help
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
long int prime[32000];
bool isprime(long int a){
for(long int i = 3; i <= 32000; i+=2){
if(a%i == 0){
return false;
}
}
return true;
}
void generateprimes(){
long int a = 0;
for(long int i = 3; i < 31623 ; i+=2){
if(isprime(i)){
prime[a] = i;
a++;
}
}
}
bool newisprime(long int a){
long int x =0;
for(long int i = prime[x]; i <= sqrt(a); i = prime[++x]){
if(a%i == 0){
return false;
}
}
return true;
}
void generateprimes_inbetween(long int n,long int m){
if(n%2 == 0){
++n;
}
if(n == 1){
printf("2\n");
n = 3;
}
for(long int i = n; i <= m ; i+=2){
if(newisprime(i)){
printf("%d\n",i);
}
}
}
int main() {
long int a,b,c;
scanf("%ld",&a);
generateprimes();
for(long int i = 0; i < a ; i++){
scanf("%ld %ld",&b,&c);
generateprimes_inbetween(b,c);
printf("\n");
}
return 0;
}
In isprime() you loop through ALL numbers in your array prime[]. But at startup, as it's global data, most of them will be 0, so that a%i will result in a fatal divide by 0.
You have somewhere to keep track of the numer of primes that you've stored in your array and only test against the primes that you've stored there.
Supposing that it's homework and you're not allowed to use vectors, you could do it as follows:
const size_t max_primes = 32000; // avoid hard coded values
unsigned long prime[max_primes] {2, 3}; // prefilled values
size_t nprimes = 2; // number of primes in the array
bool isprime(unsigned long a){
for(size_t i = 0; i < nprimes; i++){
if(a%prime[i] == 0)
return false;
}
return true;
}
void generateprimes(){
nprimes = 2;
for(unsigned long i = 3; nprimes<max_primes && i < ULONG_MAX; i += 2){
if(isprime(i)){
prime[nprimes] = i;
nprimes++;
}
}
}
bool newisprime(unsigned long a){
size_t x = 0;
for(unsigned long i = prime[x]; i <= sqrt(a) && x<nprimes; i = prime[++x]){
if(a%i == 0)
return false;
}
if(x == nprimes) {
cout << "Attention: Reaching end of prime table !!" << endl;
}
return true;
}
Some remarks:
for the index, it's safer to use the unsigned type size_t.
make sure that whenever you use an index, it remains within the bounds
as you work with positive numbers, it could make sense to use unsigned long

Minimum difference in an array

I want to find the minimum difference among all elements of an array. I read through various other questions, but couldn't find the exact source of the error in the following code.
#include<iostream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
void quicksort(long int *lp, long int *rp);
int main()
{
int t,n;
long int s[5000];
cin>>t;
while(t--){
cin>>n;
for(int i=0;i<n;i++) cin>>s[i];
quicksort(&s[0],&s[n-1]);
//cout<<"passes:"<<passes<<endl;
//for(int i=0;i<n;i++) cout<<s[i]<<" ";
long int min = abs(s[1]-s[0]);
//cout<<endl<<min;
for(int i=1;i<(n-1);i++){
long int temp = abs(s[i]-s[i+1]);
if (temp <= min) min = temp;
}
cout<<min;
}
}
void quicksort(long int *lp,long int *rp){
int arraysize= (rp-lp)+1;
if(arraysize > 1){
long int *pivot = (lp+(arraysize/2));
long int swap=0;
long int *orgl = lp;
long int *orgr = rp;
while(lp!=rp){
while (*lp < *pivot) lp++;
while (*rp > *pivot) rp--;
if (lp == pivot) pivot=rp;
else if (rp == pivot) pivot=lp;
swap = *lp;
*lp = *rp;
*rp = swap;
if((*lp == *pivot) || ( *rp == *pivot)) break;
}
quicksort(orgl,pivot-1);
quicksort(pivot+1,orgr);
}
}
The problem statement is given in this link : http://www.codechef.com/problems/HORSES
Can you please identify the error in my program ?
You are using C++ so instead of using your custom quicksort which is not really guarantee O(n*logn) you better use sort from <algorithm>.
This logic looks good:
long int min = abs(s[1]-s[0]);
//cout<<endl<<min;
for(int i=1;i<(n-1);i++){
long int temp = abs(s[i]-s[i+1]);
if (temp <= min) min = temp;
}
By the way:
cout<<min;
Add cout<<min << endl;
The line
if((*lp == *pivot) || ( *rp == *pivot)) break;
is wrong. Consider
5 4 7 5 2 5
^
pivot
Oops.
This line
long int temp = abs(s[i]-s[i+1]);
is unnecessarily complex. Since the array is (supposedly) sorted,
long int temp = s[i+1] - s[i];
does the same without calling abs.
sort(s, s + n); // #include <algorithm> O(n*log n)
Otherwise sort/find minimum algorithm is correct. There are O(n) algorithms based on randomization, bucket sort.
#include <algorithm> // sort()
#include <iostream>
int main() {
using namespace std;
int ntests, n;
const int maxn = 5000; // http://www.codechef.com/problems/HORSES
int s[maxn];
cin >> ntests; // read number of tests
while (ntests--) {
cin >> n; // read number of integers
for (int i = 0; i < n; ++i) cin >> s[i]; // read input array
sort(s, s + n); // sort O(n*log n)
// find minimal difference O(n)
int mindiff = s[1] - s[0]; // minn = 2
for (int i = 2; i < n; ++i) {
int diff = s[i] - s[i-1];
if (diff < mindiff) mindiff = diff;
}
cout << mindiff << endl;
}
}
#include <iostream> using namespace std;
int main() {
int a[] = {1,5,2,3,6,9};
int c=0;
int n = sizeof(a)/sizeof(a[0]);
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
cout<<a[i]<<" - "<<a[j]<<" = "<<a[i]-a[j]<<endl;
if(abs(a[i]-a[j]) == 2)
c++;
}
}
cout<<c<<endl;
return 0; }