<commit_before>// Created by cschalk on 3/20/16.
// Challenge 39 : Happy Number
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;

std::stack<int> foundNumbers;

int sumOfDigitSquares(int in) {
    std::stack<int> digits;
    int sum = 0;
    while(in > 0) {
            digits.push(in % 10);
            sum += (digits.top() * digits.top());
            in /= 10;
        }
    return sum;
}

bool haveWeSeenThisNumberBefore(int number) {
    std::stack<int> looped;
    while(!foundNumbers.empty()) {
        if (number == foundNumbers.top()) {
            return true;
        } else {
            looped.push(foundNumbers.top());
            foundNumbers.pop();
        }
    }
    looped.push(number);
    foundNumbers = looped;
    return false;
}

int isHappy(int number) {
    if ( number == 1 ) {
        return 1;
    } else if ( haveWeSeenThisNumberBefore(number)) {
        return 0;
    }
    return isHappy(sumOfDigitSquares(number));
}

int main(int argc, char ** args) {
    std::ifstream input {args[1]};
    int number;

    while(input >> number) {
        std::cout << isHappy(number) << std::endl;
        while(!foundNumbers.empty()) { // clean out found numbers;
            foundNumbers.pop();
        }
    }

}<commit_msg>Fixed Happy Numbers: Used vector instead of stack to reduce memory usage.<commit_after>// Created by cschalk on 3/20/16.
// Challenge 39 : Happy Number
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

std::vector<int> foundNumbers;

int sumOfDigitSquares(int in) {
    std::vector<int> digits;
    int sum = 0;
    while(in > 0) {
            digits.push_back(in % 10);
            sum += (digits.back() * digits.back());
            in /= 10;
        }
    return sum;
}

bool haveWeSeenThisNumberBefore(int number) {
    if (std::find(foundNumbers.begin(), foundNumbers.end(), number) != foundNumbers.end()) {
        return true;
    } else {
        foundNumbers.push_back(number);
        return false;
    }
}

int isHappy(int number) {
    if ( number == 1 ) {
        return 1;
    } else if ( haveWeSeenThisNumberBefore(number)) {
        return 0;
    }
    return isHappy(sumOfDigitSquares(number));
}

int main(int argc, char ** args) {
    std::ifstream input {args[1]};
    int number;

    while(input >> number) {
        std::cout << isHappy(number) << std::endl;
        foundNumbers.clear();
    }

}
{'fraction_non_alphanumeric': 0.13900245298446443, 'fraction_numerical': 0.013900245298446443, 'mean_word_length': 2.4319775596072932, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '783866', 'n_tokens_mistral': 844, 'n_tokens_neox': 783, 'n_words': 193}