<commit_before>//===-- problems/Problem2.cpp - An invalid problem --------------*- C++ -*-===//
//
// ProjectEuler.net solutions by <NAME>
//
// This file is distributed under the MIT License. See LICENSE for details.
//
//===----------------------------------------------------------------------===//
///
/// \class problems::Problem2
/// \brief Even Fibonacci numbers
///
/// Question
/// --------
/// Each new term in the Fibonacci sequence is generated by adding the previous
/// two terms. By starting with 1 and 2, the first 10 terms will be:
///
/// > 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
///
/// By considering the terms in the Fibonacci sequence whose values do not
///  exceed four million, find the sum of the even-valued terms.
///
/// Brute Force
/// -----------
/// Calculate each Fibonacci number and just sum them up.
///
/// Speeding it up
/// --------------
/// Note that every third Fibonacci number is even because we start with odd
/// numbers and require two consecutive odds to make the next number even.
///
/// By definition, the standard Fibonacci numbers are defined as:
/// \f[
///    F_{n} = F_{n-1} + F_{n-2}
/// \f]
/// Thus we want \f$F_{n+3}\f$ in terms of things we can easily calculate.
/// \f{eqnarray*}{
///   F_{n+3} &=& F_{n+2} + F_{n+1}\\
///   &=& F_{n+1} + F_{n} + F_{n} + F_{n-1}\\
///   &=& F_{n} + F_{n-1} + F_{n} + F_{n} + F_{n-1}\\
///   &=& 3 F_{n} + 2 F_{n-1}
/// \f}
/// This allows us to go from \f$n\f$ to \f$n+3\f$, but we still need
/// \f$F_{n-1}\f$ and hence \f$F_{n+2}\f$.
/// \f{eqnarray*}{
///   F_{n+2} &=& F_{n+1} + F_{n}\\
///   &=& F_{n} + F_{n-1} + F_{n}\\
///   &=& 2 F_{n} + F_{n-1}
/// \f}
/// Now we can skip straight to the even Fibonacci numbers and don't need to
/// check for even-ness, assuming correct starting values.
//===----------------------------------------------------------------------===//
#include "Problem2.h"

#include <sstream>
using std::stringstream;
#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

string problems::Problem2::answer() {
  if (!solved)
    solve();

  stringstream ss;

  ss << "The sum of all even valued Fibonacci numbers below 4 million is "
     << sum;

  return ss.str();
}

std::string problems::Problem2::description() const {
  return "Problem 2: Even Fibonacci numbers";
}

void problems::Problem2::solve() {
  sum = faster(4000000);
  solved = true;
}

// Goes through Fibonacci numbers one by one and adds only if even
unsigned int problems::Problem2::brute_force(const unsigned int limit) {
  unsigned int nm2 = 1; // n - 2
  unsigned int nm1 = 1; // n - 1
  unsigned int n = 2;   // n
  unsigned int s = 0;

  while (n < limit) {
    if (n % 2 == 0)
      s += n;

    nm2 = nm1;
    nm1 = n;
    n = nm1 + nm2;
  }

  return s;
}

// Goes through only the even Fibonacci numbers
unsigned int problems::Problem2::faster(const unsigned int limit) {
  unsigned int s = 0;
  unsigned int np3 = 2; // n + 3
  unsigned int np2 = 1; // n + 2
  unsigned int n = 0;   // n
  unsigned int nm1 = 0; // n - 1

  while (np3 < limit) {
    s += np3;

    n = np3;
    nm1 = np2;
    np2 = 2 * n + nm1;
    np3 = 3 * n + 2 * nm1;
  }
  return s;
}<commit_msg>Minor formatting<commit_after>//===-- problems/Problem2.cpp - An invalid problem --------------*- C++ -*-===//
//
// ProjectEuler.net solutions by <NAME>
//
// This file is distributed under the MIT License. See LICENSE for details.
//
//===----------------------------------------------------------------------===//
///
/// \class problems::Problem2
/// \brief Even Fibonacci numbers
///
/// Question
/// --------
/// Each new term in the Fibonacci sequence is generated by adding the previous
/// two terms. By starting with 1 and 2, the first 10 terms will be:
///
/// > 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
///
/// By considering the terms in the Fibonacci sequence whose values do not
///  exceed four million, find the sum of the even-valued terms.
///
/// Brute Force
/// -----------
/// Calculate each Fibonacci number and just sum them up.
///
/// Speeding it up
/// --------------
/// Note that every third Fibonacci number is even because we start with odd
/// numbers and require two consecutive odds to make the next number even.
///
/// By definition, the standard Fibonacci numbers are defined as:
/// \f[
///    F_{n} = F_{n-1} + F_{n-2}
/// \f]
/// Thus we want \f$F_{n+3}\f$ in terms of things we can easily calculate.
/// \f{eqnarray*}{
///   F_{n+3} &=& F_{n+2} + F_{n+1}\\
///   &=& F_{n+1} + F_{n} + F_{n} + F_{n-1}\\
///   &=& F_{n} + F_{n-1} + F_{n} + F_{n} + F_{n-1}\\
///   &=& 3 F_{n} + 2 F_{n-1}
/// \f}
/// This allows us to go from \f$n\f$ to \f$n+3\f$, but we still need
/// \f$F_{n-1}\f$ and hence \f$F_{n+2}\f$.
/// \f{eqnarray*}{
///   F_{n+2} &=& F_{n+1} + F_{n}\\
///   &=& F_{n} + F_{n-1} + F_{n}\\
///   &=& 2 F_{n} + F_{n-1}
/// \f}
/// Now we can skip straight to the even Fibonacci numbers and don't need to
/// check for even-ness, assuming correct starting values.
//===----------------------------------------------------------------------===//
#include "Problem2.h"

#include <sstream>
using std::stringstream;
#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

string problems::Problem2::answer() {
  if (!solved)
    solve();

  stringstream ss;

  ss << "The sum of all even valued Fibonacci numbers below 4 million is "
     << sum;

  return ss.str();
}

std::string problems::Problem2::description() const {
  return "Problem 2: Even Fibonacci numbers";
}

void problems::Problem2::solve() {
  sum = faster(4000000);
  solved = true;
}

// Goes through Fibonacci numbers one by one and adds only if even
unsigned int problems::Problem2::brute_force(const unsigned int limit) {
  unsigned int nm2 = 1; // n - 2
  unsigned int nm1 = 1; // n - 1
  unsigned int n = 2;   // n
  unsigned int s = 0;

  while (n < limit) {
    if (n % 2 == 0)
      s += n;

    nm2 = nm1;
    nm1 = n;
    n = nm1 + nm2;
  }

  return s;
}

// Goes through only the even Fibonacci numbers
unsigned int problems::Problem2::faster(const unsigned int limit) {
  unsigned int s = 0;
  unsigned int np3 = 2; // n + 3
  unsigned int np2 = 1; // n + 2
  unsigned int n = 0;   // n
  unsigned int nm1 = 0; // n - 1

  while (np3 < limit) {
    s += np3;

    n = np3;
    nm1 = np2;
    np2 = 2 * n + nm1;
    np3 = 3 * n + 2 * nm1;
  }

  return s;
}
{'fraction_non_alphanumeric': 0.23273179838207841, 'fraction_numerical': 0.02955818294959552, 'mean_word_length': 1.9613081529249194, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2473575', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2169, 'n_words': 783}