the error result which tells me 55440 * 13 = 720719

I try to write a small program to tell the minimum number which can be divided by 1, 2, 3....20. and here is the program, this is not the key point, but this program give an error result when I use mingw32 and the windows7 utimate OS.. another platform or another compiler may works.. I want to know if there is anyone who can reproduce this error and given a reason..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * 2520 is the smallest number that can divided by each of the numbers from 1 to 10 without
 * any remainder. What is the smallest positive number that is evenly divisible by all of the 
 * numbers from 1 to 20.
 */
 #include <iostream>
#include <vector>
#include <cmath>

#define LIMIT 20

using namespace std;

vector<int> GetPrimesBelow(int upper);
bool IsPrime(int);

int main()
{
	vector<int> primes = GetPrimesBelow(LIMIT);
	int result = 1;
	for(int i = 0; i < primes.size(); i++)
	{
		// with the each primes as a target
		int prime = primes.at(i);
		for(int j = 1; ; j++)
		{
			if(pow(prime, j) > LIMIT)
			{
				cout << "Result = " <<  result << ", mul by " << pow(prime, j - 1) << endl;
				
				result *= pow(prime, j - 1);
				
				break;
			}
		}
	}
	cout << "Result = " << result << endl;
	return 0;
}

vector<int> GetPrimesBelow(int upper)
{
	vector<int> primes;
	for(int i = 2; i <= upper; i++)
	{
		if(IsPrime(i))
		{
			primes.push_back(i);
		}
	}
	return primes;
}


bool IsPrime(int num)
{
	for(int i = 2; i <= sqrt(num); i++)
	{
		if(num % i == 0)
		{
			return false;
		}
	}
	return true;
}

and here is the output..
1
2
3
4
5
6
7
8
9
Result = 1, mul by 16
Result = 16, mul by 9
Result = 144, mul by 5
Result = 720, mul by 7
Result = 5040, mul by 11
Result = 55440, mul by 13
Result = 720719, mul by 17
Result = 12252223, mul by 19
Result = 232792237
What exactly is the error?

I'm on linux, compiling with g++. During compilation I get this warning:
1
2
test2.cxx: In function ‘int main()’:
test2.cxx:21:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Output is the same as yours. [EDIT] Read more carefully this time, output is different, see jlb's post

What's exactly the problem?
Last edited on
@jlb Exactly, the 7th line should give the "Result = 720720"..
@voice The 7th line should list the result : "Result = 720720", but it is 720719 instead..
I get the following results:

Result = 1, mul by 16
Result = 16, mul by 9
Result = 144, mul by 5
Result = 720, mul by 7
Result = 5040, mul by 11
Result = 55440, mul by 13
Result = 720720, mul by 17
Result = 12252240, mul by 19
Result = 232792560


I'm using g++4.7.2 on Linux
@jlb So this is very strange, this seems to be a precise lost, I guess...But there is no reason to say so... I mean that this program can be right..
Oh, okay I tried to compile on visual studio 2010 this time and it complains:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Build started 3/9/2013 7:42:45 PM.
PrepareForBuild:
  Creating directory "c:\documents and settings\voice\my documents\visual studio 2010\Projects\d6dsaf65\Debug\".
InitializeBuildStatus:
  Creating "Debug\d6dsaf65.unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  stdafx.cpp
  d6dsaf65.cpp
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(21): warning C4018: '<' : signed/unsigned mismatch
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(27): error C2668: 'pow' : ambiguous call to overloaded function
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or       'double pow(double,int)'
          while trying to match the argument list '(int, int)'
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(29): error C2668: 'pow' : ambiguous call to overloaded function
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or       'double pow(double,int)'
          while trying to match the argument list '(int, int)'
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(31): error C2668: 'pow' : ambiguous call to overloaded function
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or       'float pow(float,int)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or       'double pow(double,int)'
          while trying to match the argument list '(int, int)'
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(57): error C2668: 'sqrt' : ambiguous call to overloaded function
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(589): could be 'long double sqrt(long double)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(541): or       'float sqrt(float)'
          c:\program files\microsoft visual studio 10.0\vc\include\math.h(127): or       'double sqrt(double)'
          while trying to match the argument list '(int)'
c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(65): fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(56)' was matched
  Generating Code...

Build FAILED.

Time Elapsed 00:00:04.27
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


You should probably cast the arguments of pow() and sqrt() to float or double. For example, change this line
pow(prime, j)
to this
pow((double)prime, j)

This is because the first argument should be of double type, not int.
Last edited on
@voice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
1>Build started 2013/3/9 23:56:27.
1>_PrepareForClean:
1>  Deleting file "Debug\Test.lastbuildstate".
1>InitializeBuildStatus:
1>  Creating "Debug\Test.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  main.cpp
1>d:\my documents\visual studio 2012\projects\test\test\main.cpp(21): warning C4018: '<' : signed/unsigned mismatch
1>d:\my documents\visual studio 2012\projects\test\test\main.cpp(31): warning C4244: '*=' : conversion from 'double' to 'int', possible loss of data
1>Link:
1>  Test.vcxproj -> d:\my documents\visual studio 2012\Projects\Test\Debug\Test.exe
1>FinalizeBuildStatus:
1>  Deleting file "Debug\Test.unsuccessfulbuild".
1>  Touching "Debug\Test.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:08.30
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Although it complains, but the result is OK...
@voice If I don't forget, int can be automatically cast to double
Your program should be correct, your output should be as you expect. You are dealing with int so there should be no precision loss unless you are overflowing the size of your int, which I doubt. But to be sure you could change your type to long and see if you get the same results.
Topic archived. No new replies allowed.