Math
Math

Reputation: 157

The Euler website 8th work

This is my approach to the Euler website 8th problem. I know that because of its intricated loops is not minimal. But, I would like to know why it works for small numbers and not works for big ones. Say, for the number wanted in the Euler problem, it returns 8502282159491788800 that is a false answer. My guess is that the problem is an incorrect usage of 'long long' type. How can I repair this code?

the Euler website 8th problem:

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

My code:

  // Euler website 8th problem.
#include <iostream>
#include <string>

bool alter(long long  a, long long b) {
  // returns true when two numbers differe in 1.
  bool _default = true;
  if (a != b + 1 and a != b - 1) {
  _default = false;
    }
  return _default;
  }

long long prod(long long a, long long b) {
  // computes \( a * \cdots * b \) or \( b * \cdots * a \).
  if (a > b) {
    long long  cng = a;
    a = b;
    b = cng;
    }
  long long _tmp = 1;
  for (long long i = a; i <= b; i++) {
    _tmp = _tmp * i;
    }
    return _tmp;
  }


int main () {
  std::string num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
  long long _n = 13;
  long long _result = 0;
  for (long long i = 0; i < (long long) num.length() - _n + 1; i++) {
    for (long long j = i; j < i + _n - 1; j++) {
      if (alter(num.at(j), num.at(j + 1))) {
        _result = prod(j, j + _n - 1);
      }
    }
  }
  std::cout << _result << std::endl;
}

Upvotes: 3

Views: 200

Answers (1)

Esa Sharahi
Esa Sharahi

Reputation: 76

You need to fix the definition of adjacent as the same as mentioned by @Johnny Mopp. So, in your first function, you must modify the if-condition as just a < b. Now, on the second function you need to do some fundamental changes. Indeed, you SHOULD do a multiplication of digits, and not a multiplication of all integers between any pair of them. This mistake is done in the main body by alter(num.at(j), num.at(j + 1)). So, in my opinion this could be a suitable code for your second function:

long long prod(std::string _str) {
  if (_str.find('0') != std::string::npos) {
    return 0;
    }
  else {
      long long _number = std::stoll(_str);
  long long _tmp, _prod = 1;
  while(_number > 0) {
    _tmp = _number % 10; 
    _prod *= _tmp;
    _number = _number / 10;
    }
    return _prod;
    }
  }

And, finally the if-statement in the main body:

      if (alter(num.at(j), num.at(j + 1)) and prod(num.substr(j, _n)) > _result) {
        _result = prod(num.substr(j, _n));
      }

Upvotes: 1

Related Questions