Problem in Finding Prime Numbers between Two numbers [closed] - c++

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 2 years ago.
Improve this question
I have to find all prime numbers between two given numbers(given in ascending order i.e small, large) I made logic such that my program starts from the given least number till the given most numbers and find factors for each number in between, if factors count are 2 i.e 1 and itself(which is a condition for a prime number), hence it is printed as prime. However I am unable to print my desired output.. can't track why(P.S I am 19 years old newbie in Programming)
#include <iostream>
using namespace std;
int main(){
int start,end;
cin>>start,end;
for(int i=start+1;i<end;++i){
int count;
for(int j=1;j<=i;++j){
if(i%j==0 || i/2==0)count++;
}
if(count==2) cout<<i<<endl;
}
return 0;
}
Input: 1 10
Expected Output:
2
3
5
7
9
Output: (nothing)

Your program has several issues.
cin>>start,end; is not going to read in 2 numbers. You need cin >> start >> end;
You are not initializing count to anything, so you invoke undefined behavior when you do count++. You need to do int count = 0;
Also, when checking if n is prime, you don't need to check for divisibility by 1 or n since this is always true.

Related

Why 2 is not showing prime? [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 4 months ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i = 2;
while(i<n){
if(n%i==0){
cout<<"not Prime";
break;
}else{
cout<<"Prime";
break;
}
i++;
}
return 0;
}
This code is written for showing prime or composite/notPrime numbers but 2 is prime & why it is not showing in output?
I write this code for getting for identifying that given number is prime or not. It can work on any number/digit but it can't show about "2" . So why is it?
Because 2 is special number it's even prime and you have to add special case for 2.
for more info: https://mathworld.wolfram.com/EvenPrime.html#:~:text=The%20unique%20even%20prime%20number,%22oddest%22%20prime%20of%20all.
Your program will not work for 2 because the condition goes false and it will never enter to the loop!
Update
Look here for primality test
https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

Strongest Neighbour question in GeekForGeeks [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 1 year ago.
Improve this question
Input:
n = 6
arr[] = {1,2,2,3,4,5}
Output: 2 2 3 4 5
Explanation: Maximum of arr[0] and arr[1]
is 2, that of arr[1] and arr[2] is 2, ...
and so on. For last two elements, maximum
is 5.
A standard array problem and I know the right solution to it too but I tried using the max() function in the C++ std library and I'm getting this
For Input:
6
1 2 2 3 4 5
your output is:
22345
This is how my function looks like
void maximumAdjacent(int sizeOfArray, int arr[]){
for (int i = 0; i<sizeOfArray-1; i++) {
cout << std::max(arr[i+1], arr[i]) << "";
}
}
On submission this answer isn't accepted and I can't seem to figure out why?
This might be a dumb answer but it looks like you are missing spaces between the numbers. I see the "" in your string and you might need a " " instead. Without the space, it is one giant number. Does that help?

What's the problem with this recursive case? [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 2 years ago.
Improve this question
guys I faced a problem with the recursive case of a power of a number given its base and exponent. Why is it not working properly and returns 0 everytime?
#include <bits/stdc++.h>
using namespace std;
int qn(int n,int q)
if(q==1)
return 0;
return n*qn(n,q-1);
}
int main() {
cout << qn(2,2);
}
You return 0 at the end of the recursion, and multiply with that value. Will always yield 0.
Seriously, this is very easy to figure out using only a pencil and some paper.
qn(2, 2) called
q is 2 so you execute n * qn(n, q - 1)
Replacing values you have 2 * qn(2, 1)
qn(2, 1) called
q is 1, so if (q == 1) is true
thus 0 is returned
You remplace qn(2, 1) by 0 in 2 * qn(2, 1)
You have 2 * 0
That give 0
If you use a debugger, this is even easier as you can execute the program step by step and simply note everythings it does.
By the way, as written, your code would not even compile because an opening { is missing after int qn(int n,int q).

Reducing Run Time C or C++ [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 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.

Binary Conversion in c++ [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 8 years ago.
Improve this question
In order to convert a given number to binary I wrote this code
//Binary conversion
int num,count=0;
int bi[15];
cout<<"Enter number";
cin>>num;
while(num>=1){
bi[count]=num%2;
num=num/2;
count++;
}
for(int i=0;i<=count;i++){
cout<<bi[count-i];
}
But the answer is wrong.It gives a -85993460 at the front.
If I want to convert 10 the result would be -859934601010.
Can someone please point out what's wrong with this code
When i is zero, the expression count-i is one position after the last entry of the array; this is undefined behavior, so an arbitrary number, such as -85993460, can be printed, or the program could crash.
To print your array backwards, use bi[count-1-i] instead, and end the loop upon reaching count:
for(int i=0 ; i != count ; i++) {
cout<<bi[count-1-i];
}
Your loop limits are off-by-one - the loop should be
for(int i=1;i<=count;i++){
cout<<bi[count-i];
}