Check for primality number [closed] - c++

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.

Related

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

I get an error: lvalue required as left operand of assignment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I get this error, and i'm a beginner in this coding so i don't know much. Here is my program:
#include <iostream> 1
using namespace std; 2
int a,b,k,i,n,c; 3
int main() 4
{ 5
cin>>a>>b; 6
k=0; 7
for (i=a;i<=b;i=i+1) 8
{ 9
n=i; 10
c=0; 11
while (n>0) 12
{ 13
if (n%2=1) 14
c=c+1; 15
n=n/10; 16
} 17
if (c>0) 18
k=k+1; 19
} 20
cout<<k; 21
22
return 0; 23
} 24
The error appears to be at row 14!
I'm using Code::Blocks Version 13.12
You have used a single '=' on row 14, but that is only for assignment. You are doing an if comparison, so you must use '=='.
if (n % 2 == 1)

How can I split integer inside an array? [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 6 years ago.
Improve this question
In C++, I have the following input: 12345
How can I achieve this output: 1 2 3 4 5 ?
Another example could be:
input: 123
output: 1 2 3
You can simply do this:
int num = 123;
std::vector<int> digits;
while( num > 0 ) {
digits.push_back(num % 10);
num /= 10;
}

How to find the height of the deepest full level in a binary search tree? [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 7 years ago.
Improve this question
For example, given the tree:
10
5 15
0 6 12 20 // full
-5 2 8 14 16 22
4 18 24
26
The value returned by the function highestFull(BinaryNodeX<Comparable> *t) would be 3 as the height of the deepest full level is three.
If a node has no left or right node, you know that the deepest full level is 1 - the node itself.
If it has left and right nodes, recurse and choose the smaller.
highestFull(BinaryNodeX<Comparable> *t)
{
if ( ! t->left || ! t->right ) return 1;
return 1 + std::min( highestFull(t->left), highestFull(t->right) );
}