Project Euler #20 help

Hello Everyone! I was looking for some help with project Euler #20...I have gotten a number in the correct ball park, but it is still wrong. I was curious if someone could take a look at my code and tell me what i could tweak to fix the problem.

So this is what i have :

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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

double factorial(double);

double factorial(double x) {
 	double temp;
	if (x <= 1) return 1;
	
	temp = x * factorial(x - 1);
	return temp;
}

int main() {
	 double tmp = factorial(100);

	std::vector<int> vec;

	std::ostringstream strs;
	strs << std::fixed << tmp;
	std::string str = strs.str();

	for (std::string::size_type i = 0; i < str.size(); ++i) {
		vec.push_back(str[i]);
		//std::cout << str[i] << std::endl;
	}

	double out = 0;
	
	for (std::vector<int>::size_type i = 0; i < vec.size(); ++i) {
		out += (vec[i] - 48);
		//std::cout << vec[i] - 48 << std::endl;
	}
	

	std::cout << out << std::endl;
}


This gives me the answer of 681 and i need it to say 640 (if i remember the correct answer). Also any tips for C++ would be nice since I am teaching myself the language.

Also i have it has a double since a double can hold a much larger number than an int or a Long long...the trailing zeros aren't a problem since it shouldn't effect the final answer (if it is crap.)
Last edited on
why are you putting all the digits into a vector instead of just using the string?

You could simply do something like;

1
2
3
4
5
6
std::ostringstream strs;
strs << temp;
for(char const &digit : strs.str())
    sum += digit - '0';

std::cout << sum << std::endl;


I personally used boost instead of double though. With double you are probably not getting the right answer. I just confirmed you are getting the wrong factorial with double.

Also, your "out" should be an int not a double.

A way to get it to work is possibly to get all the digits without the leading zeros.

Hint there is a way to remove 24 of the digits (leading 0's)
ex: anything * 10, * 100, * 1000, * 10000 will only add a 0 to the end.

Though it will still be 134 digits
Last edited on
Thanks for the help giblit! I was using vector because i wasn't thinking...I'm reading accelerated c++ and it was a new tool given to me and my brain kinda derped.
This is what i have now. I'm getting a compile error, but i think it is because i'm missing a file or two, but XCODe isn't picking an error up before compiling. Im just curious i'm on the right track now

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
#include <iostream>
#include <string>
#include <sstream>
#include <boost/multiprecision/mpfr.hpp>

namespace mp = boost::multiprecision;
typedef mp::number<mp::mpfr_float_backend<300> > my_float;
	

my_float factorial(my_float);

my_float factorial(my_float x) {
 	my_float  temp;
	if (x <= 1) return 1;
	
	temp = x * factorial(x - 1);
	return temp;
}

int main() {
	my_float tmp = factorial(100);

	int out = 0;

	std::ostringstream strs;
	strs << tmp;
	for (char const &digit : strs.str() )
		out += digit - '0';

	std::cout << out << std::endl;
}
Last edited on
Why are you using boost/multiprecision/mpfr_float? This is for floating numbers not integers. I would just use boost/multiprecision/cpp_int

Also, you prototype before the main function and define it after. Not both before. If you do it before there is no need for a prototype.
Noted for sure. Sorry for being a little bit of a fool.
Guessing you got it working then?

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
#include <iostream>
#include <string>
#include <sstream>
#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;
typedef mp::cpp_int my_int;
	

my_int factorial(my_int);

my_int factorial(my_int x) {
 	my_int  temp;
	if (x <= 1) return 1;
	
	temp = x * factorial(x - 1);
	return temp;
}

int main() {
	my_int tmp = factorial(100);

	int out = 0;

	std::ostringstream strs;
	strs << tmp;
	for (char const &digit : strs.str() )
		out += digit - '0';

	std::cout << out << std::endl;
}
Yup, Thank you for all your help :)
Topic archived. No new replies allowed.