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 8 years ago.
Improve this question
I'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
num = std::round(num / 4.0) * 4.0;
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items
Related
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 months ago.
Improve this question
For example I want to do:
I have 1 it is 00000001 in binary.
and when I shift like 1<<3 I'll take 8 it is 00001000.
So, I need that I take 00001111 after 1<<3.
I wish you understand if something wrong add unclear ask about it please.
I want to do shorter this part:
for(int i=1;h>0;h--,i*=2) hr+=i;
As I understand, you want
std::uint32_t my_shift(std::uint32_t n, std::uint32_t lshift)
{
return (n << lshift) | ((1 << lshift) - 1);
// original shifted | 0b0001111 (lshift 1)
}
You can directly iterate hr:
for(int hr=1; h>0; h--, hr=2*hr+1)
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 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.
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 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11
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 9 years ago.
Improve this question
I have a signal x(t). I must shift it a/2 and -a/2 and then take the sum of these two shifted signals. How can I write shift of x(t) in time in C++?
For time series data a shift in time is just an offset of n samples, so to sum data which is shifted by +/-a/2 samples:
for (i = a/2; i < N - a/2; ++i)
{
y[i] = x[i - a/2] + x[i + a/2];
}