Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Let's see how good /prog/ really is

Name: Anonymous 2012-10-26 13:44

Can you find the smallest number that fulfills the following criteria?

1. It can only contain digits of 3, 5 and 7
2. The sum of the digits can be divided by 3, 5 and 7 without remainder
3. The number can be divided by 3, 5 and 7 without remainder

Name: Anonymous 2012-10-26 18:15

>>9
Code I'm using to manually test: (yes, sepples)

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;

const int arr[3] = {3, 5, 7};
const int LCM = 105;

int sumDigits(const vector<int> &v)
{
    int r = 0;
    for(int i = 0; i < (int)v.size(); ++i)
        r += v.at(i);

    return r;
}

void toVector(const string &s, vector<int> &v)
{
    for(int i = 0; i < (int)s.size(); ++i)
        v[i] = (int)(s.at(i) - '0');

    return;
}

long long vectorToNum(const vector<int> &v)
{
    long long r = 0;

    for(int i = (int)v.size() - 1, k = 0; i >= 0; --i, ++k)
        r += (long long)( (int)v.at(k) * pow(10.0, i) );

    return r;
}

bool validDigit(const int &n)
{
    return ( (n==arr[0]) || (n==arr[1]) || (n==arr[2]) );
}

bool validNumber(const vector<int> &v)
{
    int i, sum;
    long long n;

    //check #1
    for(i = 0; i < (int)v.size(); ++i)
        if( !validDigit((int)v.at(i)) ) return false;

    //check #2
    n = vectorToNum(v);
    cout << "n: " << n << endl;
    for(i = 0; i < 3; ++i)
    {
        if( (n % arr[i] != 0) ) return false;
    }
   
    //check #3
    sum = sumDigits(v);
    if( sum != LCM ) return false;

    return true;
}

int main()
{
    // smallest so far = 33577577777777775

    string ts = "33577577777777775";
    vector <int> tv ((int)ts.size());
    toVector(ts, tv);

    cout << "Number: " << ts << endl <<
        "Valid: " << validNumber(tv) << endl << endl;

    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List