Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I want to store dates in a 16 bit in a way like:
High Bytes: Y Y Y Y Y Y Y M
Low Bytes: M M M D D D D D
With the values being in the following ranges
Year: 0 - 99
Month: 1 - 12
Day: 1 to 31
I realize that it would be something like this
byte a = (year << 10) + (month << 6) + day.
Encode:
((year - year0) << 9) + (month << 5) + day
Decode:
year = (date >> 9) + year0;
month = (date >> 5) & 15;
day = date & 31;
(year<<9) | (month<<5) | day;
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 month.
The community is reviewing whether to reopen this question as of 2 days ago.
Improve this question
I had come across a code solution to check whether any number if prime or not.
int isPrime(int N){
if(N<=1){
return 0;
}
if(N==2 || N==3){
return 1;
}
if(N%2==0 or N%3==0){
return 0;
}
for(int i=5;i*i<=N;i=i+6){
if(N%i==0 || N%(i+2) ==0){
return 0;
}
}
return 1;
}
Can anyone explain, why we are increasing i=i+6 and condition N%(i+2)==0??
We are checking N%i==0 then why we are checking it for i+2?
Starting from 4 every second number is not prime. Starting from 6 every third number is not prime. Now consider what is left
4 5 6 7 8 9 10 11 12 13 14 15 16 17
2+x*2 x x x x x x x
3+y*3 x x x x
others 5 7 11 13 17 19 ...
The pattern continues and has a length of 2*3 = 6.
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 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
No matter how I see this problem, I keep getting the answer to be 10. When I run the program, the answer gives me 20. I am lost as to how this should be solved. Could anyone help me with much detail? Thanks! ps. first programming class in c++.
int n = 0;
for (int i = 1; i <= 5; i++)
for (int j = 0; j < i; j++)
n = n + j;
Let's see n after each iteration of i.
n = prev value of n + sum of 0 to i-1;
i=1 , n= 0
i=2 , n= 0+0+1=1
i=3, n= 1+0+1+2 =4
i=4, n= 4+0+1+2+3=10
i=5, n= 10+0+1+2+3+4 =20
When i == 1 you add 0 to n, so n == 0.
When i == 2 you add 0 and 1 to n, so n == 0 + 0 + 1 == 1.
When i == 3 you add 0, 1, 2 to n, so n == 1 + 0 + 1 + 2 == 4.
When i == 4 you add 0, 1, 2, 3 to n, so n == 4 + 0 + 1 + 2 + 3 == 10.
When i == 5 you add 0, 1, 2, 3, 4 to n, so n = 10 + 0 + 1 + 2 + 3 + 4 == 20
If you only got 10, you either missed the last iteration of the outer loop (because 10 is the result after 4 iterations), or you forgot that you're adding on to what you accumulated in the previous iterations (since the last iteration adds 10 to the total).
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 want to sum digits of an integer. If I have 123 I want to sum 1+2+3. I found a program that works well but I don't understand how it works. Can you explain how it works?
#include <iostream>
using namespace std;
int main()
{
int n, sum;
cout << "Enter integer\n";
cin >> n;
sum = n/100 + (n/10)%10 + n%10;
cout << "sum = " << sum << endl;
}
How n/100 generates 1 from 123, (n/10)%10 2 from 123 and n%10 3 from 123
sum = n/100 + (n/10)%10 + n%10;
1) n/100
(n=123)
in this statement 123/100 means ans is = 1
2)(n/10)%10
here (123/10) firstly evaluate and ans is = 12, and then 12%10 gets evaluate and ans is = 2
3)n%10 again 123%10 evaluate ans is 3
then statement becomes
sum = 1 + 2 + 3
Note: % symbol gives remainder
These are very simple maths.
Here, n = 123
100 | 123 | 1
100
_________
23
Therefore the quotient is 1.
The same way, for 123/10,
10 | 123 | 12
10
___________
23
20
___________
3
So, 123/10 = 12. Now for (123/10)%10 = 12%10,
10 | 12 | 1
10
______
2
Therefore, (123/10)%10 = 12%10 = 2.
The same way, 123%10 = 3
Therefore, the answer is: 123/100 + (123/10)%10 + 123%10 = 1 + 2 + 3 = 6
(Note: a % b, here b divides a and returns the remainder)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to know all possible combinations from 0-40 in sets of 2. So for example:
1 / 1 -
1 / 2 -
1 / 3 -
1 / 4 -
1 / 5 -
1 / 6 -
1 / 7 -
1 / 8 -
1 / 9 -
1 / 10
All the way to 40 and all possible combinations.
Cheers.
In haskell:
[(x, y) | x <- [1..40], y <- [1..40]]
In other languages you should probably look at for-loops: (this is C#)
Tuple<int, int>[,] things = new Tuple<int, int>[40,40];
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
things[i,j] = Tuple.Create(i+1,j+1);
}
}