Discussion about PRIMEDST

#include <bits/stdc++.h>
using namespace std;
int prime(int subject){
  if(subject == 1){
    return 0;
  }
  if(subject == 2){
    return 1;
  }
  if(subject == 3){
    return 1;
  }
  if(subject == 4){
    return 0;
  }
  if(subject == 5){
    return 1;
  }
  for(int i = 2; i <= (subject/2 + 1); i++){
    if(subject % i == 0){
      return 0;
    }
  }
  return 1;
}
int factorial(int subject){
  if(subject == 0){
    return 1;
  }
  if(subject == 1){
    return 1;
  }
  int answer = 1;;
  for(int i = 1; i <= subject; i++){
    answer = answer * i;
  }
  return answer;
}
int main(){
  int n;
  cin >> n;
  int matrix[n + 1][n + 1];
  for(int i = 0; i <= n; i++){
    for(int j = 0; j <= n; j++){
      matrix[i][j] = 191919;
    }
  }
  for(int i = 1; i <= n; i++){
    matrix[i][i] = 0;
  }
  for(int i = 1; i < n; i++){
    int firstvar;
    int secondvar;
    cin >> firstvar;
    cin >> secondvar;
    matrix[firstvar][secondvar] = 1;
    matrix[secondvar][firstvar] = 1;
  }
  float totalprime = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      if(i != j){
        for(int k = 1; k <= n; k++){
          if(k != i){
            if(k != j){
              matrix[i][j] = min((matrix[i][k] + matrix[k][j]), matrix[i][j]);
              matrix[j][i] = matrix[i][j];
            }
          }
        }
      }
    }
  }
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      if(j != i){
        if(prime(matrix[i][j]) == 1){
          totalprime = totalprime + 1;
        }
      }
    }
  }
  totalprime = totalprime/2;
  float answer1 = factorial(n)/(2 * factorial(n - 2));
  float answer_finale = totalprime/answer1;
  cout << answer_finale;
  return 0;
}

My Code is getting SIGFPE error. Please help me find answer.

int statement wrong prime(int subject)[

clrscr()
}

SIGFPE is the easiest runtime error to debug - it is a floating point error. It is virtually always caused by a division by 0, so check any divisions or modulo operations in your code carefully.

1 Like

Hi, but I have checked all the divisions and made factorial(0) = 1 and factorial (1) = 1 and then we can do the factorials from 2 onwards with the help of the function… So is there any other areas that I should check? Thank you for the help.

Can you share link to your submission?

Here’s the log when your code is compiled using Debug flags and executed.

Compiling RTE.cpp using Debug Flags
Now Running RTE.cpp in Debug Mode
RTE.cpp:35:12: runtime error: signed integer overflow: 479001600 * 13 cannot be represented in type 'int'
RTE.cpp:84:31: runtime error: division by zero
AddressSanitizer:DEADLYSIGNAL
=================================================================
==16856==ERROR: AddressSanitizer: FPE on unknown address 0x55be44d7e162 (pc 0x55be44d7e162 bp 0x7fff2c0f1d10 sp 0x7fff2c0d6c80 T0)
    #0 0x55be44d7e161 in main /home/suman/Temp/RTE.cpp:84
    #1 0x7fa054a580b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #2 0x55be44d7c40d in _start (/home/suman/Temp/exe+0x540d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: FPE /home/suman/Temp/RTE.cpp:84 in main
==16856==ABORTING
suman@Skynet:~/Temp$

If you don’t get it, check the line 84 in your code.

float answer1 = factorial(n)/(2 * factorial(n - 2));

Your code crashes there due to a Floating-point error.

2 Likes

Hi, I corrected the error., but I am getting wrong answer now… Can anybody give me the test cases for this problem. PRIMEDST

https://www.codechef.com/viewsolution/56744695
This is the link.
Thank you for the help in advance.