combinations for numbers 0 to 40 [closed] - combinations

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);
}
}

Related

Check for primality number [closed]

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.

Need Explanation for C++ output [closed]

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
#include <iostream>
int main()
{
int i = 100,sum=0;
for(int i =0; i!=10;++i)
sum+=i;
std::cout<<i<<" "<<sum<<std::endl;
return 0 ;
}
I'm a beginner in C++ , the output of the code is 100 45 . I understand 100 as its block scope but why 45?
As sum is declared in the outer scope and not redefined inside your loop, the loop is operating on the outer sum which means it's value ends up equivalent to the cumulative value of the loop scoped i which would be:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
The i variable is instead redefined in the loop scope (int i=0;), therefore while in the loop block it goes from 0 to 9, but once out of the loop the i variable is taken into account is the one with 100 assigned.
Next time, if you have the tools, I'd recommend stepping through the loop with a debugger and having a look at what the variables and values are doing.
You display i and sum right after it.
At the start of your loop i=100 and as you declare another i in the scope of your loop, when the code goes out of the scope of your loop, it display the value of the first i, that is 100 and sum = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.

Any other way to write a loop for multiplication table to take run time values? [closed]

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

Nested for() in c++ [closed]

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).

Storing a date in a 16 bits [closed]

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;