Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:09:01+00:00 2026-05-24T14:09:01+00:00

I wrote up a solution for a puzzle-type website. On XCode with the latest

  • 0

I wrote up a solution for a puzzle-type website. On XCode with the latest g++, my code compiles fine. On their website (and codepad), my output is negative. Could someone help me understand why, because I’m honestly stumped.

#include <iostream>
#include <cmath>
#include <vector>
#include <map>

using namespace std;

vector<int> getAllPrimes(vector<int> primesArray, int n)
{
    vector<int> numArray (n+1, 1);

    for (int i = 2; i <= n; i++)
    {
        if (numArray[i] == 1)
        {
            primesArray.push_back(i);
            for (int k = i; k <= n; k+= i)
            {
                numArray[k] = 0;
            }
        }
    }

    return primesArray;
}

int main()
{
    long n = 32327;

    if (n == 1)
    {
        printf("%ld\n", n);
        return EXIT_SUCCESS;
    }


    map <int, int> primeMap;
    map <int, int>::iterator itr;
    vector<int> primesArray;

    primesArray = getAllPrimes(primesArray, n);

    while(!primesArray.empty())
    {
        long currPrime = primesArray.back(), curr = currPrime;
        while (currPrime <= n)
        {
            primeMap[curr] += (int)floor(n/currPrime);
            currPrime *= curr;   //multiply currPrime to add another factor of curr. 
        }
        primesArray.pop_back();
    }

    //get the number of divisors of n!
    long numDivisors = 1;
    for (itr=primeMap.begin(); itr != primeMap.end(); itr++)
    {
        numDivisors *= ((*itr).second*2)+1;     //power of each prime + 1, * 2 because you need the number of divisors of the square of n!
        numDivisors = numDivisors % 1000007;
    }

    printf("%ld\n", numDivisors);

    return 0;
}

Ordinarily “long n” is supposed to read an integer between 1 and 1 million from standard input, but I just assigned a value to simulate it.

I have put the code in codepad here: http://codepad.org/RpPFuLzX. As you can see, the output is -596936, whereas on my machine it is 656502 (which is the correct output). What exactly is going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:09:01+00:00Added an answer on May 24, 2026 at 2:09 pm

    Very likely culprit is that CodePad and the other site are compiling on a 32-bit system, where a long is 4 bytes long (http://codepad.org/W00vCFIN). On OS X, everything defaults to 64-bit, and on those systems (but not Windows), a long is 8 bytes long. As such, you are overflowing a computation at some point.

    If you depend on a specific integer size, use stdint.h

    Here is an adapted version which matches your expected output, using an int64_t: http://codepad.org/Owsl3ClR

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main solution is using code from a utility class library, that I wrote
I wrote a simple script to solve a logic puzzle, the type of puzzle
You're given a solution to a Sudoku puzzle. Write the code to check if
Data comes in by email as a zipped file. The Java solution we wrote
A while back I wrote a simple python program to brute-force the single solution
I wrote myself a little downloading application so that I could easily grab a
EDIT 23/11 : Solution founded ... I wrote $_db instead $this->_db in the constructor
I have a little Problem but don't know the Solution! I have wrote a
I wrote a stupid solution for this, any better recipe? As you can see
while I wrote this question I figured out a solution but I'm going to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.