Created
August 31, 2015 18:04
-
-
Save codepainkiller/6c4f62468a6a8f964228 to your computer and use it in GitHub Desktop.
Sum Of Digits
This file contains hidden or 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 <iostream> | |
| #include <vector> | |
| #include <cmath> | |
| using namespace std; | |
| bool esPrimo(int numero) | |
| { | |
| int res=false; | |
| int divs=2; | |
| int mitad; | |
| if (numero>2) | |
| { | |
| mitad=floor((double)numero/2); | |
| do | |
| { | |
| res=(numero%divs!=0); | |
| divs++; | |
| } while ((res!=0) && (divs<=mitad)); | |
| } | |
| else if (numero==2) | |
| res = true; | |
| return res; | |
| } | |
| int sumOfDigits() | |
| { | |
| int sumaDigitos = 0; | |
| int sumaPrimos = 0; | |
| vector<int> vect; | |
| int cantidad = 0; | |
| int i = 2; | |
| while (cantidad < 100) | |
| { | |
| if (esPrimo(i)) | |
| { | |
| vect.push_back(i); | |
| cantidad++; | |
| } | |
| i++; | |
| } | |
| // Suma de de numeros | |
| for(int i = 0; i < vect.size(); i++) | |
| { | |
| cout << vect.at(i) << ","; | |
| sumaPrimos += vect.at(i); | |
| } | |
| // Suma de los digitos | |
| int x = sumaPrimos; | |
| do{ | |
| sumaDigitos += x %10; | |
| x /= 10; | |
| }while(x > 0); | |
| return sumaDigitos; | |
| } | |
| int main() | |
| { | |
| cout << endl << sumOfDigits() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment