print prime number upto number N - c++14

#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int flag=1;
for(int i=2;i<=N;i++){
for(int j=2;j<i;j++){
if(i%j==0){flag=0;break;}
break;
}
if(flag==1){cout<<i<<" ";}
}
return 0;
}
program is to print prime numbers upto input number N with seperated space
ex - N=9, my o/p -2 3,
expected o/p - 2 3 5 7
help

Just initial flag = 1 after every iteration
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int flag=1;
for(int i=2;i<=N;i++){
flag=1;
for(int j=2;j<i;j++){
if(i%j==0){flag=0;break;}
break;
}
if(flag==1){cout<<i<<" ";}
}
return 0;
}

You've got at least two issues:
You never reset flag to 1. So as soon as a non-prime is found, everything is non-prime. You want to set it back to 1 each time you loop in the outer loop.
You break out of your inner loop unconditionally (j is never anything but 2), so you're actually testing for "is odd" not "is prime". Only break if you've confirmed it's not prime.
Fix that, and the code works:
#include<iostream>
using namespace std;
int main() {
int N;
cin>>N;
for(int i=2;i<=N;i++){
int flag=1; // Set each loop
for(int j=2;j<i;j++){
if(i%j==0){
flag=0;
break; // Only break when confirmed composite
}
// no break otherwise
}
if(flag==1){
cout<<i<<" ";
}
}
return 0;
}
Try it online!

Related

write a program that calculates the number of comparisons in binary insertion sorting?

I would like to know how I can write a program to calculate the number of comparison in binary insertion sorting
I tried to code the binary insertion program below, but I would like to know how I can calculate the overall comparisons made.
#include<iostream>
using namespace std;
int binarysearch (int a[], int sel, int high, int low){
int mid=(high+low)/2;
if(high<=low){
if(sel>a[high]){
return high+1;
}
else{
return high;
}
}
else{
if(sel==a[mid]){
return mid+1;
}
else if(sel>a[mid]){
return binarysearch( a, sel, high, mid+1);
}
else{
return binarysearch( a, sel, mid-1, low);
}
}}
void insertionsort(int a[], int n){
for(int i=1; i<n; i++){
int j=i-1;
int sel=a[i];
int loc=binarysearch(a,sel,j,0);
while(j>=loc){
a[j+1]=a[j];
j--;
}
a[j+1]=sel;
}
}
int main(){
int a[]= {1,6,2,5,3,4};
int n=sizeof(a)/sizeof(a[0]);
insertionsort(a,n);
cout<<"Sorted array is :";
for (int i = 0; i < n; i++)
cout<<a[i]<<"\t";
return 0;
}

Geeksforgeeks is showing my code as runtime error after submission

This is my code
#include <bits/stdc++.h>
using namespace std;
int srch(vector<int> arr, int ln, int fn)
{
for (int i = 1; i <= ln; i++)
{
if (arr[i] == fn)
return i;
}
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
vector<int> a(n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
printf("%d\n", srch(a, n, k));
}
return 0;
}
I am not understanding where is the problem. Here is the problem link Click here.
Please help me solving this problem. I am not understanding why geeksforgeeks is showing runtime error for this code.
Note that your loops use n-th entry of vector, but
vector< int> a(n);
has indexes from 0 to n-1
Notice that the first element has a position of 0 (not 1).

Why is this code not working? Output is not being printed

When I compiled in devc++ compiler, output is not printed. Is there any logical error? This program should accept a number from the user, print the lowest palindrome greater than the given number.
It is a problem from SPOJ PALIN
include
using namespace std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{long long int i;
long long int k;
int flag=1;
long long int n;
int a[10000000];
cin>>n;
n++;
start:
i=0;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;}
i--;
k=i;
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{flag=0;break;}
else
i--;
}
if(flag==1)
cout<<n<<endl;
else
{n++;goto start;}
}
return 0;
}
You have not initialized the flag variable. if(a[i]==a[j]) then the flag will remain uninitialized and may create problems while cheking the if condition unless the default value will be 0 for it.(I am not sure that it would be). Also I can see that the value of flag will never turn 1 according to your code.
#include<iostream>
using namespace::std;
int main()
{
int t;
cin>>t;
cout<<endl;
while(t--)
{
long int i, k, n; int a[1000]; int flag = 1, duplicaten;
cin>>n;
n++;
start:
//cout<<"inside start : n = "<<n<<"\n";
flag=1; i=0;
duplicaten=n;
while(n!=0)
{
a[i]=n%10;
i++;
n=n/10;
}
n=duplicaten;
i--;
k=i;
//cout<<"number of digits of n = "<<i+1<<"\n";
for(int j=0;j<=k;j++)
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
i--;
}
//cout<<"flag = "<<flag<<endl;
if(flag==1)
{
//cout<<"FOUND!!\n";
cout<<n<<endl;
}
else
{
n++;
//cout<<"NOT found\n";
goto start;
}
}
return 0;
}
You can change the size of your data types yourself and atleast vote up this answer.
< Output before using duplicaten : link to image >

QuickSort Algorithm implementation

I am not getting where i am going wrong implementing quicksort algorithm.
Below is the code:
#include <bits/stdc++.h>
using namespace std;
int part(vector<int> &arr,int i,int j)
{
int pivot=i;
i++;
while(i<j)
{
while(arr[i]<arr[pivot])
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
swap(arr[i],arr[j]);
}
swap(arr[j],arr[pivot]);
return j;
}
void quickSort(vector <int> &arr,int p,int r) {
if(p<r)
{
int t=part(arr,p,r);
quickSort(arr,p,t-1);
quickSort(arr,t+1,r);
}
}
int main()
{
int n;
cin >> n;
vector <int> arr(n);
for(int i = 0; i < (int)n; ++i) {
cin >> arr[i];
}
quickSort(arr,0,arr.size()-1);
for(int i=0;i<arr.size();i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
i am giving input as
7
5 8 1 3 7 9 2
but getting output as :
2 1 3 7 5 8 9
Can anyone please point out where i am going wrong.
In the "part" function you're swapping at the end, even if the values are already in place.
Just check the values before swapping:
int part(vector<int> &arr, int i, int j)
{
int pivot = i;
i++;
while (i < j)
{
while (arr[i] < arr[pivot])
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j)
swap(arr[i], arr[j]);
}
if (arr[j] < arr[pivot]) {
swap(arr[j], arr[pivot]);
}
return j;
}
If i'm not mistaken, the condition in
if(i<j)
swap(arr[i],arr[j]);
in the function part is not correct; it should check the relation of the array values arr[i] and arr[j] instead of i and j to decide whether the array entries are to be swapped.

Unable to find what's wrong with my code for solve spoj CWC15

I am unable to find what's going wrong in both memotization and tabulation for spoj http://www.spoj.com/problems/CWC2015/.If you could point why both codes are giving respective errors that would be really helpful.
1--Memotization
Error--time limit exceeded.
Don't know why generated random cases and tested on ideone most of the solutions are coming out in less than a second.
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cstring>
using namespace std;
#define max 20000000
int a[40];
int n;
int m;
long long sum1;
bool dp[40][max];
int solve(long long sum,int x,int k)
{
if(sum==0)
{
if(k==m)
{
return true;
}
else
{
return false;
}
}
else if(x==n)
{
return false;
}
else if(dp[x][sum])
{
return dp[x][sum];
}
else
{
return dp[x][sum]=(solve(sum,x+1,k)||solve(sum-a[x],x+1,k+1));
}
}
int main()
{
int t;
scanf("%d",&t);
for(int l=1;l<=t;l++)
{
scanf("%d",&n);
m=n/2;
long long sum=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Case %d: ",l);
if(n%2)
{
printf("No\n");
continue;
}
if(sum%2)
{
printf("No\n");
continue;
}
sum=sum/2;
if(solve(sum,0,0))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
2-tabulation
Error-Sigsegv(Segmentation fault)
I know segmentation fault can be caused by taking an array of too big a size.
But the code works perfectely on ideone.
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cstring>
using namespace std;
#define max 20000000
int a[40];
int n;
long long sum;
bool dp[max+1][41];
bool solve()
{
int k=0;
//cout<<"sum is "<<sum<<endl;
for (int i = 0; i <= n; i++)
dp[0][i] = true;
for (long long i = 1; i <= sum; i++)
dp[i][0] = false;
for (long long i = 1; i <= sum; i++)
{
for (int j = 1; j <= n; j++)
{
dp[i][j] = dp[i][j-1];
if (i >= a[j-1])
dp[i][j] = dp[i][j] || dp[i - a[j-1]][j-1];
if(i==sum && dp[i-a[j-1]][j-1])
{
k+=1;
}
}
}
/*cout<<k<<endl;*/
return (dp[sum][n] && k==n/2);
}
int main()
{
int t;
scanf("%d",&t);
for(int l=1;l<=t;l++)
{
scanf("%d",&n);
sum=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Case %d: ",l);
if(n%2)
{
printf("No\n");
continue;
}
if(sum%2)
{
printf("No\n");
continue;
}
sum=sum/2;
if(solve())
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
Note-In both programs k is keeping track of number of included elements in the solution so that I can tell whether distribution is equal in terms of number of players or not.If these approaches are wrong a hint towards right direction would be much appreciated.
Suggestion:
The way you are solving will not work because of complexity. Although space complexity will work (limit is 1536MB and space used is around 785MB), but time complexity is too high for time limit of 5s. An estimate is 10^8 could be executed safely within 1 sec. If you submit only initialization part of your code, it will exceed time limit(i have done that to verify).
To Solve it:
you do not need to travel all sum from 1 to 200 00 000, rather just iterate on generated sum when including ith player.
Lets say 4 players are there with experience 2 3 4 5.
You do not need to travel for sum: 1 to 8, rather do something like this:
Initial sum 0
if you include 1st element: 0 & 2
if you include 2nd element: 0, 2, 3, 4
if you include 3rd element: 0, 2, 3, 4, 6, 7
etc.
Now this way it could upto 2^N. So maintain a map of int of 20000000 and do not put a number in queue, if it is already there. This will solve your problem of iteration 20000000 * 40 to iterating over just unique reachable sum values (complexity will depend of nature of input).
Now if your reach desired sum, then it is reachable. To watch for equal number of players in both teams: i have a hint for you, remember i mention "map of int of 20000000", i said int because this map could be also used to store how many numbers could reach to a particular sum. Use bitwise operator to encode this info. You just need to maintain count and not which particular element is included.
PS: I solved this problem, it took a while and it was fun.

Resources