Find the prime numbers in specific interval - c++

I have to find prime numbers for t cases. Examples of input / output below:
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
Notice the space between the answers as well.
#include <iostream>
#include <cmath>
bool prime (int x, int y);
using namespace std;
int main()
{
int t, x, y;
cin >> t;
for(int i = 0; i < t; i++)
cin >> x >> y;
for(int i = 0; i < t; i++){
for (int i = x; i <= y; i++)
cout << prime(x, y) << endl;
}
return 0;
}
bool prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++)
if (i % j == 0)
prime = false;
return prime;
}
}
My program outputs only 1 all the time, why is that?

You are outputting the result of prime(x, y), which is a bool. This will always be zero or one.

Related

A number K and an Array of size N is given, check whether we can get the sum of any two elements of array equal to K

I tried to solve the problem but my code still contains some bugs. Why isn't it running?
Here is the link of the question website: https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/pair-sums/?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int n = 1e7 + 10;
int hsh[n];
int main()
{
int n, k;
cin >> n >> k;
int A[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
for (int i = 0; i < n; i++)
{
hsh[A[i]] = k - A[i];
}
int t = 0;
for (int i = 0; i < n; i++)
{
if (hsh[A[i]] == k - hsh[hsh[A[i]]])
{
cout << "YES";
t = 1;
break;
}
}
if (t == 0)
{
cout << "NO";
}
return 0;
}
The problem is that while hsh[A[i]] is always valid, hsh[hsh[A[i]] is not.
Consider the following input:
1 1
10000
This does the following:
A[0] = 10000;
...
hsh[10000] = 1 - 10000; // = -99999
...
if (hsh[10000] == 1 - hsh[-99999]) {...}
So your code is reading out of bounds of the array hsh[]. Make sure you check first if hsh[A[i]] >= 0.
Note that your code is more complicated than necessary; you can do a single loop over the input to check if there is a matching pair:
#include <iostream>
static constexpr int max_k = 2e6;
static bool seen[max_k + 1];
int main()
{
int n, k;
std::cin >> n >> k;
for (int i = 0; i < n; ++i)
{
int A;
std::cin >> A;
if (A <= k && seen[k - A]) {
std::cout << "YES\n";
return 0;
}
seen[A] = true;
}
std::cout << "NO\n";
}

Function to find the smallest prime x and the biggest m = power_of(x) such that n % m = 0 and n % x = 0?

Given m integer from 1 to m, for each 1 <=i <= m find the smallest prime x that i % x = 0 and the biggest number y which is a power of x such that i % y = 0
My main approach is :
I use Eratos agorithm to find x for every single m like this :
I use set for more convenient track
#include<bits/stdc++.h>
using namespace std;
set<int> s;
void Eratos() {
while(!s.empty()) {
int prime = *s.begin();
s.erase(prime);
X[prime] = prime;
for(int j = prime * 2; j <= L ; j++) {
if(s.count(j)) {
int P = j / prime;
if( P % prime == 0) Y[j] = Y[P]*prime;
else Y[j] = prime;
}
}
}
signed main() {
for(int i = 2; i<= m; i++) s.insert(i);
Eratos();
for(int i = 1; i <= m; i++) cout << X[m] << " " << Y[m] ;
}
with X[m] is the number x corresponding to m and same as Y[m]
But it seems not really quick and optimal solution. And the memory request for this is so big and when m is 1000000. I get MLE. So is there an function that can help to solve this problem please. Thank you so much.
Instead of simply marking a number prime/not-prime in the original Sieve of Eratosthenes, save the corresponding smallest prime factor which divides that number.
Once that's done, the biggest power of the smallest prime of a number would mean to simply check how many times that smallest prime appears in the prime factorization of that number which is what the nested for loop does in the following code:
#include <iostream>
#include <vector>
using namespace std;
void SoE(vector<int>& sieve)
{
for (int i = 2; i < sieve.size(); i += 2)
sieve[i] = 2;
for (int i = 3; i < sieve.size(); i += 2)
if (sieve[i] == 0)
for (int j = i; j < sieve.size(); j += i)
if(sieve[j] == 0)
sieve[j] = i;
}
int main()
{
int m;
cin >> m;
vector<int> sieve(m + 1, 0);
SoE(sieve);
for (int i = 2; i < sieve.size(); ++i)
{
int x, y;
x = y = sieve[i];
for (int j = i; sieve[j / x] == x; j /= x)
y *= x;
cout << x << ' ' << y << endl;
}
}
I didn't get what you're trying to do but I understand that you're trying to use Sieve of Eratosthenes to find prime numbers. Well, what you probably need is a bitset, it's like a boolean array but uses bits instead of bytes which means it uses less memory. Here's what I did:
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
vector<int> primes;
int main()
{
const int m = 1e7;
bitset<m> bs;
int limit = (int) sqrt (m);
for (int i = 2; i < limit; i++) {
if (!bs[i]) {
for (int j = i * i; j < m; j += i)
bs[j] = 1;
}
}
for (int i = 2; i < m; i++) {
if (!bs[i]) {
primes.push_back (i);
}
}
return 0;
}

Floating Point Error With This Code Can someone help me

i am facing problem with this chinese remainder theoram
i am taking an input.txt file as input
and trying to generate an output.txt file but it says floating point error. when i am running with some specific input at that time it is working but for many test cases it is not working
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
int findMinX(int num[], int rem[], int k) {
int prod = 1;
for(int i = 1; i <= k; i++) prod *= num[i];
int result = 0;
for(int j = 1; j <= k; j++) {
int y = prod / num[j];
result = result + rem[j] * ModInverse(y, num[j]) * y;
}
return result % prod;
}
int main() {
ifstream infile;
infile.open("input.txt");
int n;
int num[100];
int rem[100];
infile >> n;
for(int i = 0; i < n; i++) infile >> num[i];
for(int i = 0; i < n; i++) infile >> rem[i];
infile.close();
int k = sizeof(num) / sizeof(num[0]);
ofstream myfile;
myfile.open("output.txt");
myfile << findMinX(num, rem, k);
myfile.close();
return 0;
}
These lines:
for(int i = 1; i <= k; i++)
for(int j = 1; j <= k; j++)
will cause i and j to go out of bounds.
Array indexing starts at 0 in C++, so you should use indexes 0 to k-1.
Do this instead:
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
Another thing worth checking up is this function:
int ModInverse(int a, int m)
{
a = a % m;
for(int x = 1; x < m; x++)
if(((a * x) % m) == 1) return x;
}
Given the wrong input, it'll exit the loop and return nothing, which causes undefined behaviour. Validate the input and print an error message if the file contains data you can't handle.
Here's an example of an input.txt that makes it crash for me:
5
1 2 3 4 5
2 3 4 5 6
Another cause for concern is that you use k instead of n in your call to the function:
myfile << findMinX(num, rem, k);
This means the function will always work on 100 values. Some of them may be uninitialized, and again, undefined behaviour.

Trouble passing 2D array in C++ 8 Puzzle

For some reason I can't get the distance function to work. Could someone explain to me what I am doing wrong?
Main Function:
#include <iostream>
#include <cmath>
using namespace std;
int distance(int**, int);
int main()
{
int m;
int goal [3][3] = { {1,2,3},{4,5,6},{7,8,0}};
const int N = 3;
int intial[N][N];
cout << "Enter in values: \n";
for(int i=0; i<3; i++) //This loops on the rows.
{
for(int j=0; j<3; j++) //This loops on the columns
{
cin >> intial[i][j];
}
}
cout << goal[0][0] << "\n" ;
m = distance(intial,N);
system("PAUSE");
return 0;
}
Manhattan Distance calculation!
int distance(int** array,int N)
{
int MSum = 0;
for (int x = 0; x < N; x++){ //Transversing rows(i)
for (int y = 0; y < N; y++) { //traversing colums (j)
int value = array[x][y]; // sets int to the value of space on board
if (value != 0) { // Don't compute MD for element 0
int targetX = (value - 1) / N; // expected x-coordinate (row)
int targetY = (value - 1) % N; // expected y-coordinate (col)
int dx = x - targetX; // x-distance to expected coordinate
int dy = y - targetY; // y-distance to expected coordinate
MSum += abs(dx) +abs(dy);
}
}
}
int m = MSum;
return m;
}
This looks basically correct as far as logic goes. The array you pass in should be compatible with the function declaration and implementation. Here is a sample declaration:
int distance(int array[3][3],int N);

Finding prime numbers in specific interval

Find prime numbers in specific interval in specific amount of test cases.
Example is below:
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
Notice the little space between the answer also.
here's my code:
#include <iostream>
#include <cmath>
void prime (int x, int y);
using namespace std;
int main()
{
int t, x[10], y[10];
cin >> t;
for (int i = 0; i < t; i++)
//for (int j = 0; j < t; j++)
cin >> x[i] >> y[i];
while (t > 0){
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
t--;
}
}
void prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++){
prime = true;
if (i % j == 0)
prime = false;
}
if (prime == true)
cout << i << endl;
}
cout << endl;
}
Here's the output I get when I use the same input.
1
2
3
5
7
10
3
5
1
2
3
5
7
10
What am I doing wrong?
You should move prime = true outside of the for loop. You are currently resetting it at every iteration. As far as the printing, you don't need that << endl when you print each line. You only need a space.
Since no one's pointed it out yet, if you're wondering why you're getting three sets of output instead of two...
while (t > 0){
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
t--;
}
might be better phrased as
for (int i = 0; i < t; i++)
prime(x[i], y[i]);
(The outer loop is what gives you extra output.)