Member Avatar

Hello, friends. I did the fourth challenge on Project Euler and I'd like to know what I can do to improve the efficiency of my code. Right now it takes 1.562s to run and the source code is as follows: #include <iostream> #include <string.h> using namespace std; int Number = 0; int Reversed_Number = 0; int First_Number = 0; int Second_Number = 0; int Product = 0; int Biggest_Palindrome = 0; bool Is_Palindrome = false; string Text_Valued_Number = ""; string Text_Valued_Reversed_Number = ""; void Reverse_Number (int); void For_Loop (int,int); void Check_if_Number_is_a_Palindrome(int,int); void Show_Number(int); void For_Loop(int First_Number, int Second_Number) { for …

Member Avatar
+0 forum 5
Member Avatar

Hey, guys. Find the sum of all the primes below 2000000. My code: *main.cpp* #include <iostream> #include <vector> #include <cmath> #include "Sieve.h" using namespace std; typedef unsigned long long int ulli; ulli Sum = 0; ulli Sum_of_Primes(vector<bool>, ulli&); ulli Sum_of_Primes(vector<bool> Primes, ulli& Sum) { for(ulli a = 2; a < Upper_Limit; a++) { if (Primes[a]) { Sum += a; } } return Sum; } int main() { Find_Primes_until_n(Primes); cout << Sum_of_Primes(Primes,Sum) << endl; return 0; } *Sieve.h* #ifndef SIEVE_H_INCLUDED #define SIEVE_H_INCLUDED using namespace std; #define Upper_Limit 2000000 vector<bool> Primes; void Find_Primes_until_n (vector<bool>&); void Find_Primes_until_n(vector<bool>& Primes) { Primes.resize(Upper_Limit); fill(Primes.begin(),Primes.begin() + Upper_Limit …

Member Avatar
+0 forum 4
Member Avatar

Hey, guys. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a² + b² = c². For example, 3² + 4² = 9 + 16 = 25 = 5². There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. #include <iostream> using namespace std; int a = 0; int b = 0; int c = 0; bool Found = false; int Find_Triplet(int,int,int); int Find_Triplet(int a, int b, int c) { for (int i = 5; i < 1000; i++) { c = i; …

Member Avatar
+0 forum 8
Member Avatar

Hello, guys. How would I deal with a variable that needs to be biggers than an unsigned long long integer? Thank you.

Member Avatar
+0 forum 2
Member Avatar

Hello, guys. This challenge asks us to find the 10001-th prime. Here's my code: // A reasonable approximation is to say that the n-th prime is such that p(n) = n*(log n + log*(log n) - 1). // With that in mind, we can say that the 10001-th prime is smaller than 10100*(log 10100 + log(log 10100) - 1), which we can round to 105000. #include <iostream> #include <cmath> #define MAX_INTERVAL 105000 using namespace std; int Wanted_Prime = 0; void Show_Prime(int); void Show_Prime(int Wanted_Prime) { cout << "The 10001-th prime is " << Wanted_Prime << endl; } int main() { …

Member Avatar
+0 forum 9
Member Avatar

Hello, guys. The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? Here's how I did it: #include <iostream> #include <string> #include <vector> using namespace std; string Grid_of_Numbers = "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" …

Member Avatar
+0 forum 7
Member Avatar

Hello, guys. Here's the challenge: 2520 is the smallest number that can be 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? I wrote my code: #include <iostream> #include <cmath> using namespace std; #define MAX_VALUE 2793510720 unsigned int Smallest_Evenly_Divisable = 0; void Show_Number(unsigned int); void Show_Number(unsigned int Smallest_Evenly_Divisable) { cout << "\n\n" << Smallest_Evenly_Divisable << endl; } int main() { unsigned i = 0; for (i = 2520; i < MAX_VALUE; i++) { if ( i % …

Member Avatar
+0 forum 7
Member Avatar

Hello, guys. I've wrote the following code #include <iostream> #include <string.h> #include <string> #include <locale> #include <stdio.h> #include <stdlib.h> #include <limits> #include <vector> #include <windows.h> using namespace std; string Numero_de_Matricula = ""; string Nome = ""; int Numero_de_Materias = 0; int Numero_de_Creditos = 0; vector<double> creditos (10); vector<double> Coeficientes(10); double Coeficiente_Total = 0; bool repetir = false; void ClearScreen(); void Perguntar_Matricula_e_Nome(string,string); void Perguntar_Materias_e_Creditos(); void Perguntar_Coeficientes(int); void Calcular_Media(vector<double>&,int); bool Repetir(bool); void ClearScreen() { HANDLE hStdOut; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD count; DWORD cellCount; COORD homeCoords = { 0, 0 }; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); if (hStdOut == INVALID_HANDLE_VALUE) return; /* Get the …

Member Avatar
+0 forum 6
Member Avatar

Hello, guys. I wrote the code to a Quadratic Equations Solver and I'd like for you to run the program and read the source code and give me tips on what I can improve, what is good and what is bad, suggestions, etc. . #include <iostream> #include <locale.h> #include <math.h> #include <cmath> #include <complex> #include <windows.h> #include <conio.h> #include <limits> using namespace std; float a = 0; float b = 0; float c = 0;float x1 = 0; float x2 = 0; double Delta = 0; bool repetir = false; int i = 0; int Sim_ou_Nao = 0; void Instrucoes(bool); …

Member Avatar
+0 forum 2
Member Avatar

Hello everyone. I saw a post of one of our friends here about a problem he found of the SPOJ website. So I went there to check it. I found it to be very cool; I started to do the exercises but when I got in the 2nd, a prime number generator, I got stuck. I did made the program and it works as it should, however, it's execution time is too long. So I'm coming here to ask for you to help me make my program more efficient. Here's my source code: #include <iostream> #include <fstream> using namespace std; …

Member Avatar
+0 forum 17
Member Avatar

Hello everyone, my name is Petcheco and I've been coding C and C++ codes for a while. I already came to this website a couple times and I think it's great. It's a pleasure to meet you all and I hope I can assist and be assisted as much as possible, :D.

Member Avatar
+0 forum 10

The End.