Created
June 2, 2012 13:26
-
-
Save Mjiig/2858432 to your computer and use it in GitHub Desktop.
Project Euler #56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <sstream> | |
#include <algorithm> | |
class Hugenum | |
{ | |
public: | |
std::string str; //stores the number in decimal form, this is a horrible way to store numbers, but it works for almost any size we can imagine | |
Hugenum(int initial); | |
void add(Hugenum operand); | |
int digitcount(); | |
}; | |
Hugenum::Hugenum(int initial) | |
{ | |
// std::iostream = std::stringstream(str) << initial; | |
std::stringstream ss; | |
ss<<initial; | |
str=ss.str(); | |
std::reverse(str.begin(), str.end()); | |
} | |
//operand must not be bigger than the current value | |
void Hugenum::add(Hugenum operand) | |
{ | |
int carry=0; | |
size_t i; | |
if(operand.str.length() > str.length()) | |
{ | |
std::cout << "You've done someting horribly wrong" << std::endl; | |
exit(EXIT_SUCCESS); | |
} | |
for(i=0;i<operand.str.length(); i++) | |
{ | |
int a = str[i]-'0'; | |
int b = operand.str[i] -'0'; | |
int result= a+b+carry; | |
carry=result/10; | |
result=result%10; | |
str[i]='0'+result; | |
} | |
//This doesn't work... | |
while(carry!=0) | |
{ | |
if(i<str.length()) | |
{ | |
int a = str[i]-'0'; | |
int result=a+carry; | |
carry=result/10; | |
result=result%10; | |
str[i]='0'+result; | |
i++; | |
} | |
else | |
{ | |
str+= '0'+carry; | |
carry=0; | |
} | |
} | |
} | |
int Hugenum::digitcount() | |
{ | |
int ret=0; | |
for(size_t i=0; i< str.length(); i++) | |
{ | |
ret+=str[i]-'0'; | |
} | |
return ret; | |
} | |
int main() | |
{ | |
int max=0; | |
for(int i=1; i<=100;i++) | |
{ | |
Hugenum a(i); | |
for(int j=1; j<=100; j++) | |
{ | |
//Calculate i^j | |
Hugenum b=a; | |
for(int k=1; k<i; k++) | |
{ | |
a.add(b); | |
} | |
if(a.digitcount()>max) | |
{ | |
max=a.digitcount(); | |
} | |
} | |
} | |
std::cout << max <<std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment