Jump to content

Need help with notation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
So I am currently working through the Project Euler problems. The only problem I have is that I suck hardcore at math but I am trying. Below is my code for this problem Problem 6 - Project Euler.

The result I keep getting is -2.51642e+007, and if I reverse the order of subtraction I get the reverse. My problem is that I have no clue what number that is as the shorthand notation used perplexes me. I moved the decimal place 7 spaces right for both positive and negative results and that is not correct.

Anyone know what I am doing wrong?

// SquaresandSums.cpp : Defines the entry point for the console application.

// Finds the difference between the sum of the squares of numbers 1 - 100, and the squared sum of numbers 1 - 100


#include "stdafx.h"

#include "math.h"

#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])

{

	double count = 0;  //Holds the iterations count

	double sum = 0;    //Holds the sum of the iterations

	double sqsum = 0;  //Holds the square of each sum added together

	double temp = 0;   //For the purpose of squaring each iteration of count

	double total = 0;  //Holds the difference between the sum of squares and the squared sum


	while(count < 100)	//Execute 100 loops

	{

		count++; //Increase count by one each time the loop iterates

		cout << count << "\n";

		sum = sum + count;

		cout << sum << "\n";

		temp = pow(count, 2);

		cout << temp << "\n";

		sqsum = sqsum + temp;

		if( count == 100)

		{

			sum = pow(sum, 2);

			total = sqsum - sum;

			cout << "The resulting difference is..." << total;

			cout << "Exit now? (y/n)";

			cin >> temp;

		}

	}


	return 0;

}


#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
You can pass "fixed" to cout to get "normal" notation for floating point numbers. And #include <iomanip> and use "setprecision" to control the precision. Like this:

float myfloat = 4.14642;
std::cout << "Incoming float!\n" << std::fixed << std::setprecision (1) << myfloat;
Still, your code does not work very well. I recommend you do it like this:

#include <iostream>
#include <iomanip>

int main()
{
    // Find sum:
    int sum = 0;
    for(int i = 1; i <= 10; ++i)        
        sum += i;
    std::cout << "Sum:\t" << sum << std::endl;
    
    // Square of sum:
    float sqsum = sum * sum;
    std::cout << "Sum^2:\t" << std::fixed << sqsum << std::endl;
    
    // Find sum of squares
    float sumsq = 0;
    for(int i = 1; i <= 10; ++i)        
        sumsq += i * i;
    std::cout << "^2 Sum:\t" << std::fixed << sumsq << std::endl;
    
    // Difference:
    std::cout << "\nDifference:\t" << std::fixed << std::setprecision (1) << sqsum - sumsq << std::endl;
        
    return 0;    
}

Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
The result -2.51642e+007 is using scientific (or standard) notation. See here for more information.

The answer is right or at least would be if you subtracted the sum of the squares from the square of the sum, but it isn't accurate as the scientific notation has been applied and then the number has been rounded to 5 decimal places.

Scientific notation is used by C++ when it encounters a value held as a double or float. While it's possible to format the output to not use this notation it's easier in this case to just use long ints instead as you have no need for the decimal precision.
If there's a new way, I'll be the first in line.

But, it better work this time.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For many Project Euler problems, you will need a high-precision number library. I've used GMP for quite a few problems. C/C++ just doesn't have enough decimal places on its ints to do the calculations.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thanks for the responses there. I will look at GMP for the precision library to use with the problems there. I had thought I had done it right and once I included the std::fixed into my cout I was able to find the correct answer. Thanks again for the help.