Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
So the program must compute all the numbers that can be written as a sum of a power of 2, a power of 3 and a power of 5 below 5.000.000.
For example 42 = 16 + 1 + 25 = 2^4 + 3^0 + 5^2. Any idea how can I do this?
you can get all powers of 2 and all powers of 3 and all powers of 5 under 5.000.000. first Then you can try all combinations
vector<int> solve(){
const int M = 5000000;
vector<int> p_2={1},p_3={1},p_5={1};
while(p_2.back()*2<M)p_2.push_back(p_2.back()*2);
while(p_3.back()*3<M)p_3.push_back(p_3.back()*3);
while(p_5.back()*5<M)p_5.push_back(p_5.back()*5);
set<int> st;//to remove duplicates
for(auto power_of_2 :p_2){
for(auto power_of_3:p_3){
for(auto power_of_5:p_5){
If(power_of_2+power_of_3+power_of_5<M)
st.insert(power_of_2+power_of_3+power_of_5);
}
}
}
return vector<int>(st.begin(),st.end());
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The following piece of code outputs 0 and I'm not sure why. I'm not sure what the meaning of of . is in this context. Is it an operator or is it just indicating a float? Is it related to *Int?
#include <iostream>
using namespace std;
int main(){
int *Int = new int;
*Int = 1 / 2 * 2 / 1. * 2. / 4 * 4;
cout << *Int;
return 0;
}
It's not an operator. It indicates a double, not a float.
42. means 42.0, and .42 means 0.42. A . alone is a compiler error (rather than 0.0).
If you add a trailing f, it will become a float instead of double, e.g. 1.f, .1f, 1.0f.
1 / 2 == 0, 0 multiplied by anything is 0 again.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm trying to convert the individual contents of a string to integers. I need to take each character from the string and convert it to an integer to add to another. This is not using C++11. Is there a simple way to do it?
if the characters are numbers then the numeral value of each is
num_value(c) = c - '0'
This is only possible because the characters representing numbers are in order in the ASCII table.. All you have to do is loop across the string.
"I need to take each character from the string and convert it to an integer to add to another"
In case you want to calculate the sum of digits stored in std::string object, you could do:
std::string myNum("567632");
int sum = 0;
for (size_t i = 0; i < myNum.size(); ++i)
sum += (myNum[i] - '0');
std::cout << sum;
which outputs 29 (i.e. 5 + 6 + 7 + 6 + 3 + 2)
How about std::accumulate ?
#include<string>
#include<algorithm>
//...
std::string myNum("123456789");
std::cout<<accumulate( myNum.begin(), myNum.end(), 0,
[](int sum,const char& x){return sum+=x-'0'; });