Floating point exception: 8 on Smith number check - c++

So I have written a code to check if a long int number is Smith, but I keep getting Floating point exception: 8, no matter what size do I fix my variables in. Not quite sure what I am missing.
#include <iostream>
bool isPrime(long int k) {
if (k == 1) return false;
for (long int i = 2; i*i < k; i++)
if (k % i == 0)
return false;
return true;
}
int main(){
long int n;
std::cin >> n;
long int sumPr = 0, sumCif = 0;
while (n > 0) {
sumCif += n % 10;
n = n/10;
}
for (long int i = 0; i*i<=n/2; i++) {
if (isPrime(i)) {
while (n % i == 0){
long int p = i;
while (p > 0) {
sumPr += (p % 10);
p = p/10;
}
n = n/i;
}
}
}
if (sumPr == sumCif) std::cout << "1" ; else std::cout << "0";
return 0;
}

The limits of this loop appear to be flawed:
for (long int i = 0; i*i<=n/2; i++) {
Possibly partly due to copy and paste from isPrime(). But the larger problem is you need to modularize this code so that you can properly test each component. And reuse modules (e.g. you implement sum of digits of a number twice in your code.) Code duplication is a potential source of error.
#include <iostream>
bool isPrime(long number) {
if (number < 2) {
return false;
}
if (number % 2 == 0) {
return (number == 2);
}
for (long divisor = 3; divisor * divisor <= number; divisor += 2) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
long sum_digits(long number) {
long sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
long sum_prime_factor_digits(long number) {
long sum = 0;
for (long divisor = 2; divisor <= number; divisor++) {
if (isPrime(divisor)) {
while (number % divisor == 0) {
sum += sum_digits(divisor);
number /= divisor;
}
}
}
return sum;
}
bool is_smith(long number) {
if (isPrime(number)) {
return false; // only composites can play this game
}
return sum_digits(number) == sum_prime_factor_digits(number);
}
int main() {
long number;
std::cin >> number;
if (is_smith(number)) {
std::cout << "1";
} else {
std::cout << "0";
}
std::cout << "\n";
return 0;
}
TESTS
> ./a.out
4
1
> ./a.out
5
0
> ./a.out
6
0
> ./a.out
22
1
> ./a.out
4937775
1
> ./a.out
15966114
1
>
Writing clean code isn't something you do after the fact, it's what you do to help you in the debugging process.

Related

How to eliminate the extra asterisk shown from the prime factorization output?

The following code inputs an integer (n) from the user and outputs the prime decomposition of n. I need to have the following output (as an example), but can't reach it:
Input: 98
Output: 2*7^2
The actual wrong output, which has an extra "*" is:
2*7^2*
^
Maybe there is another solution using functions, which I don't know.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n, countA = 0, countB = 0;
cin>>n;
while(n % 2 == 0)
{
n /= 2;
countA++;
}
if(countA == 1)
cout<<2<<"*";
else if(countA != 0)
cout<<2<<"^"<<countA;
for(int i = 3; i <= sqrt(n); i = i + 2)
{
while(n % i == 0)
{
n /= i;
countB++;
}
if(countB == 1)
cout<<i<<"*";
else if(countB != 0)
cout<<i<<"^"<<countB<<"*";
}
if(n > 2)
cout<<n;
return 0;
}
Instead of unconditionally printing it:
cout<<i<<"^"<<countB<<"*";
You could test if it's the last number. Example (apply everywhere where it's needed):
for(int i = 3, end = sqrt(n); i <= end; i = i + 2) {
// ...
cout << i << '^' << countB;
if(i + 2 <= end) cout << '*';
So one of the possible solutions to my question, according to #Jonathan Leffler's comment is as follows:
#include <iostream>
#include <cmath>
using namespace std;
const char *pad = "";
int main()
{
int n, countA = 0, countB = 0;
cin>>n;
while(n % 2 == 0)
{
n /= 2;
countA++;
}
if(countA > 0)
{
cout<<pad;
cout<<2;
if(countA > 1)
{
cout<<"^"<<countA;
}
pad = "*";
}
for(int i = 3; i <= sqrt(n); i = i + 2)
{
countB = 0;
while(n % i == 0)
{
n /= i;
countB++;
}
if(countB > 0)
{
cout<<pad;
cout<<i;
if(countB > 1)
{
cout<<"^"<<countB;
}
pad = "*";
}
}
if(n > 2)
{
cout<<pad;
cout<<n;
pad = "*";
}
return 0;
}

Check if number is perfect prime recursively c++

I've been assigned to write a program that checks if a number is perfect prime or not (the sum of its digits is prime, the sum of the sum of its digits is prime...). I've stumbled upon two extreme cases that break my program:
INPUT: 20328307 OUTPUT: true (expected false)
INPUT: 587899597 OUTPUT: true (expected false)
The code:
#include <iostream>
using namespace std;
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i == n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (sum_of_digits(n) >= 10) is_perfect_prime(sum_of_digits(n)); //cas recursiu
return is_prime(n); //cas base
}
int main() {
int n;
while (cin >> n) cout << (is_perfect_prime(n) ? "true" : "false") << endl;
}
I can't see where this script fails for these two values, and why it doesn't fail for smaller numbers.
First of all your for loop is incorrect, it should be instead:
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
otherwise you return true almost on every non even number. Second you ignore result of recursive call, possible solution is:
bool is_perfect_prime(int n) {
if ( n >= 10 and not is_perfect_prime(sum_of_digits(n)) )
return false;
return is_prime(n); //cas base
}
Finally I've got it to work. The problem was in the is_prime() for loop and in the recursive case of is_perfect_prime(). This is what I've come up with:
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (n < 10) return is_prime(n);
if (! is_prime(n)) return false;
return is_perfect_prime(sum_of_digits(n));
}
Thanks for your answers.

Function to check for largest prime factor returning returning 0 when using large numbers c++

This is for the third problem in Project Euler.
I suspect that it only returns 0 because I assigned 0 to the 'returnum' variable on initialization. My code works fine in smaller numbers, but it isn't working for a large number (600851475143) which is needed to get the correct answer.
Is this for the size of int? If so what data type should I use?
Here is my code:
int problem3(long int num) {
long int returnum;
for (int i = 2; i < num; ++i) {
if (num % i == 0 && primecheck(i)) {
returnum = i;
}
}
return returnum;
}
And this is my 'primecheck' function:
bool primecheck(long int num) {
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}
Edit: I have tried to change the datatype but when I do, it just doesn't respond, showing an empty console for a few minutes, then crashing.
The maximum value for a variable of type int is 2147483647, whereas the max value of float is : 3.40282e+38 or 0x1.fffffep+127. You could use double also.
long long problem3(long long num) {
long long returnum;
for (long long i = 2; i < num; ++i) {
if (num % i == 0 && primecheck(i)) {
returnum = i;
}
}
return returnum;
}
And this is my 'primecheck' function:
bool primecheck(long long num) {
for (long long i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}
Fundamental Types (C++)
tested! OK!
VS2017
#include "stdafx.h"
#include <stdio.h>
bool primecheck(long long num);
long long problem3(long long num);
bool primecheck(long long num) {
for (long long i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
return false;
}
}
return true;
}
long long problem3(long long num) {
long long returnum;
for (long long i = 2; i < num; ++i) {
if (num % i == 0 && primecheck(i)) {
returnum = i;
}
if (i % 100000 == 0) {
printf("tick - %lld\n", i);
}
}
return returnum;
}
int main()
{
printf("out number %lld\n", problem3(600851475143));
return 0;
}
32 037 210 000 - screenshot 10 000 000 per/sec .
(in this moment > (long)2^32)
and what you want with this program? wait 65K sec? (18 hours)
may be need use math algorithm for best time ?

Checking if a number is a Simber

A simber is defined as a positive integer in which any odd digit, if present, occurs an odd number of times, 1414414 is a simber. 4 is even and it appears four time and 1 is odd and it appears 3 times.
Here are my variables:
int is_simber(int n)
{
int numberOfTimes = 0, length = 1, x = n;
bool answer;
vector <int> nmbrs = vector <int>();
//get how many digits are in the integer
do
{
x /= 10;
length++;
}
while(x != 0)
//get the digits in the integer
for(int i = 0; i<length; i++)
{
nmbrs.push_back(((n/10^i) % 10);
}
//checking how many times a digit occurs and also testing to see if the digits
//meet the requirements
for(int i = 0; i<length; i++)
{
for(int j = 0; j<length; j++)
{
if (nmbrs.at(i) == nmbrs.at(j))
{
numberOfTimes++;
}
}
if (nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 0)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 1)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 1)
{
answer = false;
break;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 0)
{
answer = false;
break;
}
}
return answer;
}
Your code has compilation errors.
^ is Binary XOR Operator. You can't expect it to produce pow(10,i). So replace nmbrs.push_back(((n/10^i) % 10); with nmbrs.push_back(((n/pow(10,i)) % 10);
When I was able to remove the compilation errors, i realized your code has logical errors too. As your given definition of simber, int is_simber(int n) should be fair and simple.
bool is_simber( int n )
{
if ( n<0 ) return false; // simber is a positive integer
int digitCount[10] = {}; // initializing all with 0;
// digitCount array holds the number of occurance of a digit
// so d[1] holds the number of times 1 occurred
// d[2] holds the number of times 2 occurred and so on ...
while( n ){
int d = n%10;
n /= 10;
digitCount[d]++;
}
// we just have to check
// any odd digit, if present, occurs an odd number of times
for( int i=1; i<=9; i= i+2) // Attention i=i+2, to iterate over just odd numbers
{
if( digitCount[i] != 0 && digitCount[i]%2 == 0 ) return false;
}
return true;
}
My definition of is_simber
inline bool is_even(int n) { return n % 2 == 0; }
bool is_simber(int n) {
if (n < 0) return false;
int digits[10] = {0};
for (; n; n /= 10) ++digits[n % 10];
for (int i = 0; i < 10; i += 2)
if (!is_even(digits[i]) && is_even(digits[i + 1])) return false;
return true;
}
LIVE DEMO

Project Euler 10 exercise

What is the sum of all the primes below 2000000?
Example of sum below 10 is 2+3+5+7 = 17
I wrote this code, but still getting the wrong answers:
I tested for numbers lower than a few hundreds, and it has shown the correct answers.
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(long n)
{
if (n < 2)
return false;
if (n == 2)
return true;
if (n == 3)
return true;
int k = 3;
int z = (int)(sqrt(n) + 1); // square root the n, because one of the product must be lower than 6, if squared root of 36
if (n % 2 == 0)
return false;
while (n % k != 0)
{
k += 2;
if (k >= z)
return true;
}
return false;
}
long primeSumBelow(long x)
{
long long total = 0;
for (int i = 0; i < x; i++) // looping for times of prime appearing
{
if (isPrime(i) == true)
total += i;
if (isPrime(i) == false)
total += 0;
}
cout << "fd" << endl;
return total;
}
int main()
{
cout << primeSumBelow(20) << endl;
cout << primeSumBelow(2000000) << endl;
system("pause");
return 0;
}
The total counter's type is correctly long long. Unfortunately the function primeSumBelow returns only long so, depending on the platform, the correctly calculated result is truncated when it's returned from this function.