Harmonic partial sum calculator with multithreading I wrote a program which computes the harmonic partial sum to N terms with multithreading capability. I've been working on this to sharpen my C++ skills for the upcoming semester. Just wondering if there's anything that doesn't follow the C++ standard and/or glaringly wrong. Also wondering if I can make any further optimizations.

Basically I have my program set up to only use threads if the input is divisible by 4, since I am using 4 threads due to my quad core CPU. I want this to be more flexible but I haven't thought of a better solution yet. If anyone has a suggestions, that'd be appreciated.

#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &lt;thread&gt;
#include &lt;ctime&gt;
#include &lt;cassert&gt;

using namespace std;

const int NUM_THREADS = 4;

// H(N) is defined to be the harmonic partial sum to N terms.
// calculateHarmonic(): function given to threads to compute H(N).
// unsigned long start: starting point for the loop; different for each thread.
// unsigned long N: amount of terms to compute; different for each thread; divided evenly by # of threads.
// double *sum: pointer to a double which holds a thread's individual sum for H(N).
void calculateHarmonic(long long start, long long N, double *sum)
{
    for(; start &lt;= N; start++)
    {
        *sum += 1.0/start;
    }
}

void executeWithThreads(long long N)
{
    thread t[NUM_THREADS];  // using four threads
    double sum[NUM_THREADS] = {0.0};    // array of each sum calculated by each thread
    double finalSum = 0.0;  
    clock_t start = clock();

    // Assign threads their task.
    for(int i = 0; i &lt; NUM_THREADS; i++)
    {
        // thread([name of function], [starting point], [N], [address of individual element in sum array])
        t[i] = thread(calculateHarmonic, (((N/4)*i)+1), ((N/4)*(i+1)), &amp;(sum[i]));
    }

    // Join threads to terminate and sum up all individual sums into finalSum.
    for(int i = 0; i &lt; NUM_THREADS; i++)
    {
        t[i].join();
        finalSum += sum[i];
    }

    clock_t end = clock();
    printf("Harmonic sum of %lld is %1.18lf\n", N, finalSum);
    printf("Calculation took %1.18lf seconds.\n", (((double)(end-start)/CLOCKS_PER_SEC))/NUM_THREADS);
}

void executeNoThreads(long long N)
{
    double sum = 0.0;
    clock_t start = clock();
    for(int i = 1; i &lt;= N; i++)
    {
        sum += 1.0/i;
    }
    clock_t end = clock();
    printf("Harmonic sum of %lld is %1.18lf\n", N, sum);
    printf("Calculation took %1.18lf seconds.\n", (((double)(end-start)/CLOCKS_PER_SEC))/NUM_THREADS);
}

int main()
{
    long long N;

    cout &lt;&lt; "Enter the amount of terms to calculate the Harmonic sum to: ";
    cin &gt;&gt; N;
    assert(N &gt; 0 &amp;&amp; "N must be a nonzero positive integer.");
    cout &lt;&lt; endl;

    if(N &gt; NUM_THREADS &amp;&amp; N % 4 == 0)
    {
        cout &lt;&lt; "Calculating with threads..." &lt;&lt; endl;
        executeWithThreads(N);
    }
    else if(N &gt; NUM_THREADS &amp;&amp; N % 4 != 0)
    {
        cout &lt;&lt; "Calculating without threads..." &lt;&lt; endl;
        executeNoThreads(N);
    }
    else
    {
        cout &lt;&lt; "Calculating without threads due to small N..." &lt;&lt; endl;
        executeNoThreads(N);
    }

    return 0;
}


Compiled with:

g++ -std=c++11 -pthread -Ofast harm_sum.cpp -o harm.out ; ./harm.out
Try not to use using namespace std in global scope.  If you must use it, keep it in local scope, such as in a function.  More info about that here.
I don't need any need to use printf() over std::cout here.  You are also using C-style casting and no std:: for clock_t, so some of this code still looks C-like.

If you're using printf() because you're having trouble with formatting, then take a look at the &lt;iomanip&gt; library for something useful.
Unless you're debugging, assert may be unnecessary.  You can easily output an error message to std::cerr and return EXIT_FAILURE from main().
The value 4 used in main() appears to be a magic number and should be a constant.
Each use of std::endl flushes the buffer and takes extra time. Luckily, you're not using it in the timed section.  It's still not needed in most cases and you can also output a newline with "\n", which doesn't do a flush as well.  More info about that here.