Thread: A problem I can't figure out at all!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This seems to be the proper test for pentagonal numbers:
    Code:
    #include <iostream>
    #include <cmath>
    
    typedef unsigned long ulong;
    
    bool checkPentagonal(ulong n)
    {
        double x = (sqrt(24.0 * n + 1) + 1) / 6;
        return floor(x) == x;
    }
    
    int main()
    {
        for (size_t i = 1; i < 2000; i++)
            if (checkPentagonal(i))
                std::cout << i << '\n';
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  2. #2
    Registered User Stormboy's Avatar
    Join Date
    Aug 2013
    Location
    Planet Earth
    Posts
    12
    Quote Originally Posted by oogabooga View Post
    This seems to be the proper test for pentagonal numbers:
    Code:
    #include <iostream>
    #include <cmath>
    
    typedef unsigned long ulong;
    
    bool checkPentagonal(ulong n)
    {
        double x = (sqrt(24.0 * n + 1) + 1) / 6;
        return floor(x) == x;
    }
    
    int main()
    {
        for (size_t i = 1; i < 2000; i++)
            if (checkPentagonal(i))
                std::cout << i << '\n';
    }

    Aww yeah!! The solution came correct when I replaced my checkPentagonal() function with yours.
    Thanks a bunch dude.

    Output: 1533776805

    The new code:
    Code:
    #include <iostream>
    #include <cmath>
     
    using namespace std;
     
    bool checkPentagonal(unsigned long somenum);
    unsigned long genTriNum(long n);
     
    int main()
    {
        unsigned long answer = 0;
        for(int i = 287; answer < 1; i += 2)
        {
            unsigned long cur_tn = genTriNum(i);
            if(checkPentagonal(cur_tn))
            {
                answer = cur_tn;
                break;
            }
        }
        cout << "Answer: " << answer << endl;
        cin.get();
        cin.ignore();
        return 0;
    }
     
    bool checkPentagonal(unsigned long somenum)
    {
        double x = (sqrt(24.0 * somenum + 1) + 1) / 6;
        return floor(x) == x;
    }
     
    unsigned long genTriNum(long n)
    {
        unsigned long nth_term = n*((n+1)/2);
        return nth_term;
    }
    And thank you all for trying to help me .

    EDIT:
    Just a quick question. What does floor() do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot figure out my problem can anyone help?
    By caffrea4 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2011, 01:50 PM
  2. figure - problem
    By jamesfisher in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 05:28 PM
  3. I cant figure the problem out!
    By jturner38 in forum C Programming
    Replies: 5
    Last Post: 03-25-2009, 01:13 AM
  4. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  5. Can'nt figure out problem
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 12-26-2005, 08:11 PM

Tags for this Thread