/* NTmach.cpp implementation of a Number Theory Machine */ #include "NTmach.h" /* set_n() ------------------------------------ * sets data member n (a.k.a. NTmach::n) to num */ void NTmach::set_n(long num){ n = num; } /* numDigits() --------------------------------- * returns the number of digits in NTmach::n * operates on a copy of n, so as not to change n */ int NTmach::numDigits() { int count = 0; //counter for digits long num = n; //copy of data member //count digits in n while(num > 0){ num = num / 10; //shift num right ++count; //bump count } //return result return count; } bool NTmach::isPerfect(){ return (sumOfDivisors() == n); } int NTmach::sumOfDivisors(){ int pd = 2, //possible divisor sum = 1; //sum of divisors //generate and test divisors while(pd <= n / 2){ if(n % pd == 0) sum += pd; pd++; } return sum; }