Prime Numbers Up To A Number - c++

Im really new to C++ and im working through the book Programming: Principles and Practice Using C++. Were working on the problem to find all prime numbers between 1 - user given number. Now I got that part down. I now understand that the sqrt(i) would make the loop shorter but, Im not sure what to check for to see if its a prime or not in my if - else statements.
#include<vector>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
vector<double> prime_numbers;
double num;
cout << "Please enter a number so we can find the primes for it: " << flush;
cin >> num;
for (int i = 2; i <= num; i++) {
for (int j = 2; j <= i; j++) {
// cout << sqrt(i) << "\t";
// Check to see if Value of i is incremented correctly
// Check to see if value of j is incremented properly before returnign to i
//cout << i <<"\t" << j << endl;
if (j == i) {
prime_numbers.push_back(i);
}
if (i % j == 0) {
break;
}
}
}
for (double x : prime_numbers)
cout << x << " | ";
return 0;
}

A very efficient way to find the prime numbers from 0 to n is using the sieve of Eratosthenes, there are many ways to do it, here is an example:
vector<bool> v(n, true);
v[0] = v[1] = false;
for (int i = 2; i*i < n; i+= 2){
if (v[i]) {
for (int k = i*i; k < n; k += i) {
v[k] = false;
}
if (i == 2)i = 1;
}
}
for(auto i = 0; i < n; ++i)
if(v[i])cout << i << ' ';
cout << endl;

The difference is that your previous primality condition ā€“ i == jā€“ is no longer true.
It is true exactly when you have examined every number from 2 to i, but with the sqrt(i) limit, you're exiting the loop much earlier.
I think the simplest change is to introduce a variable and move the push_back outside the loop (this works with either loop condition):
for (int i = 2; i <= num; i++) {
bool isPrime = true; // Assume 'i' is prime until proven wrong.
for (int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
prime_numbers.push_back(i);
}
}
That is, first decide whether something is the case, then do something with that information.
This is a useful design in very many cases.
For instance,It makes it much easier to move the primality check into a function:
bool isPrime(int x) { /* something */ }
// ...
for (int i = 2; i <= num; i++) {
if (isPrime(i)) {
prime_numbers.push_back(i);
}
}

Related

How to check if 3 elements of array have the same value

I am trying to write a program which checks if 3 (or more) elements of an array are the same.
I have written a code which works almost perfectly, but it gets stuck when there are 3 pairs of equal elements and I'm not sure how to fix it.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[10],skirt=0;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(a[i] == a[j])
{
skirt++;
}
}
}
cout<<skirt<<endl;
if(skirt>=3)
{
cout << "TAIP" << endl;
}
else
{
cout << "NE" << endl;
}
}
When I input
6
3 3 2 2 1 1 i
get "TAIP" but I need to get "NE".
You can use the following algorithm: first sort the array. Then iterate each adjacent pair. If they are equal, then increment counter, if not then reset counter to 1. If counter is 3, return true. If loop does not return true, then return false.
Add the following condition in the outer for loop
for(int i = 0; i < n - 2 && skirt != 3; i++)
^^^^^^^^^^^^^^^^^^^^^^^
{
skirt = 1;
^^^^^^^^^
for(int j = i + 1; j < n; j++)
{
if(a[i] == a[j])
{
skirt++;
}
}
}
Of course before the loop you should check whether n is not less than 3. For example
if ( not ( n < 3 ) )
{
for(int i = 0; i < n - 2 && skirt != 3; i++)
{
skirt = 1;
for(int j = i + 1; j < n; j++)
{
if(a[i] == a[j])
{
skirt++;
}
}
}
}
Here is a demonstrative program
#include <iostream>
using namespace std;
int main()
{
int a[] = { 6, 3, 3, 2, 2, 1, 1 };
int n = 7;
int skirt = 0;
if ( not ( n < 3 ) )
{
for(int i = 0; i < n - 2 && skirt != 3; i++)
{
skirt = 1;
for(int j = i + 1; j < n; j++)
{
if ( a[i] == a[j] )
{
skirt++;
}
}
}
}
cout << skirt << endl;
if ( skirt == 3 )
{
cout << "TAIP" << endl;
}
else
{
cout << "NE" << endl;
}
return 0;
}
Its output is
1
NE
because the array does not have 3 equal elements.
Reset skirt to 0 every time you increase i if it is less than 3, or break out the loop otherwise.
Another way to do this is using a std::map, which keeps a count of the number of times a given value occurs in your array. You would stop looking as soon as you have a number that has three occurrences.
Here's a 'minimalist' code version:
#include <iostream>
#include <map>
using std::cin; // Many folks (especially here on SO) don't like using the all-embracing
using std::cout; // ... statement, "using namespace std;". So, these 3 lines only 'use'
using std::endl; // ... what you actually need to!
int main() {
int n, a[10], skirt = 0;
std::map<int, int> gots;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n && skirt < 3; i++) {
skirt = 1;
if (gots.find(a[i]) != gots.end()) skirt = gots[a[i]] + 1;
gots.insert_or_assign(a[i], skirt);
}
cout << (skirt >= 3 ? "TAIP" : "NE") << endl;
return 0;
}
I'm not saying this is any better (or worse) than the other answers - just another way of approaching the problem, and making use of what the Standard Library has to offer. Also, with this approach, you could easily modify the code to count how many numbers occur three or more times, or any number of time.
Feel free to ask for further clarification and/or explanation.

How do I make a C++ int array loop that will not allow repeated values? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I need a loop that will keep asking for input until there isn't a repeated value in the array. When I run my code it will recognize that there is a duplicate but it will go through the rest of my code and not ask for input again. I've tried this many different ways so any insight is greatly appreciated
cout << "Enter 10 integers with no repeated values: ";
for (x = 0; x < n; x++)
{
cin >> d[x];
}
while (duplicateTest(d))
{
for (x = 0; x < n; x++)
{
cin >> d[x];
}
}
bool duplicateTest (int d[])
{
int i, j;
bool duplicate;
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10 + 1; j++)
{
if (d[i] == d[j])
{
cout << "Invalid entry, do not repeat values." << endl
<< "Enter 10 integers with no repeated values: " << endl;
duplicate = true;
}
else
duplicate = false;
}
}
return duplicate;
Your duplicateTest function should be rewritten to return true as soon as it finds a single duplicate:
bool duplicateTest(int (&d)[10]) {
for (int i = 0; i < 10; ++ i)
for (int j = i + 1; j < 10; j++)
if (d[i] == d[j]) {
std::cout << "Invalid entry, do not repeat values\n"
"Enter 10 integers with no repeated values:\n";
return true;
}
return false;
}
As it is right now it sets duplicate to true when it finds a duplicate but then it will probably set it back to false during a later iteration when some d[i] d[j] pair is not a duplicate
I had to do a bit of work to make your code able to compile, below is a working example:
#include <iostream>
bool duplicateTest(int (&d)[10]) {
for (int i = 0; i < 10; ++ i)
for (int j = i + 1; j < 10; j++)
if (d[i] == d[j]) {
std::cout << "Invalid entry, do not repeat values\n"
"Enter 10 integers with no repeated values:\n";
return true;
}
return false;
}
int main() {
int d[10];
std::cout << "Enter 10 integers with no repeated values:\n";
for (int i = 0; i < 10; i++)
std::cin >> d[i];
while (duplicateTest(d))
for (int i = 0; i < 10; i++)
std::cin >> d[i];
std::cout << "Done\n";
return 0;
}
In duplicateTest() After you set duplicate = true you need to return. Consider [0] and [1] are the same. You'll set duplicate = true then compare [1] and [2] which are different so duplicate will go back to false. Once you have found a duplicate you can stop looking - you don't care how many dupes you have.
Problem is you are overwriting the duplicate flag in duplicateTest().
cout << "Enter 10 integers with no repeated values: ";
for (x = 0; x < n; x++)
{
cin >> d[x];
}
while (duplicateTest(d))
{
cout << "Duplicate value found. Enter 10 integers with no repeated values: ";
for (x = 0; x < n; x++)
{
cin >> d[x];
}
}
bool duplicateTest (int d[])
{
int i, j;
bool duplicate = false; //Better to initialize.
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10 + 1; j++)
{
if (d[i] == d[j])
{
cout << "Invalid entry, do not repeat values." << endl
<< "Enter 10 integers with no repeated values: " << endl;
duplicate = true;
break; //If you detect at least one duplicate, exit the loop.
}
else
duplicate = false;
}
if(duplicate) break; //Break the outerloop.
}
return duplicate;
}

Another approach for finding longest increasing subsequence

I am aware that there are many algorithms for finding longest increasing subsequence, but the algorithms using DP all have this is common - they recurse/ dynimacally calculate longest subsequence "ENDING" at a particular element of the array.
I wrote a solution which resurses taking longest subsequence "starting" at a particular array, which seems to work fine too.
#include<iostream>
using namespace std;
#define boostio ios_base::sync_with_stdio(0);
int n;
int a[100000];
int t[100000] = {-1};
int lis(int i){
if(t[i]!= -1){
return t[i];
}
if(i == n){t[i] = 1; return 1;
}
for (int x = i+1; x <= n ; ++x)
{
if(a[x] > a[i] and 1 + lis(x) > t[i]){
t[i] = 1 + lis(x);
//return t[i];
}
}
if(t[i] != -1){
return t[i];
}
t[i] = 1; return 1;
}
int main(){
boostio;
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
fill(t, t + n+2 ,-1);
Int m = 0;
for (int i = 1; i <= n; ++i)
{
//cout << i << " " << lis(i) << endl;
if(lis(i) >m) m= lis(i);
}
cout << m;
return 0;
}
I am wondering, is this in any way worse than if we recurse on "last element" of subsequence instead of the first. They both appear to be order n square algorithms to me, so why isnt this algorithm more circulated. To me it seems more intuitive. Am I missing something?

Finalize the magic square generator

This code that runs only for odd N. The problem is that there are no ideas how to add support for even values N
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
setlocale(0, "");
int n;
cout << "Enter the size of the magic square - ";
cin >> n;
int **matrix = new int *[n];
for (int i = 0; i < n; ++i)
{
matrix[i] = new int[n];
}
int nsqr = n * n;
int i = 0, j = n / 2;
for (int k = 1; k <= nsqr; ++k)
{
matrix[i][j] = k;
i--;
j++;
if (k % n == 0)
{
i += 2;
--j;
}
else
{
if (j == n)
{
j -= n;
}
else if (i < 0)
{
i += n;
}
}
}
cout << "\n\nMagic square size - " << n << "\n\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
for (i = 0; i < n; i++)
delete[] matrix[i];
delete[] matrix;
system("pause >> null");
return 0;
}
I would be grateful for tips on troubleshooting.
If i'm not mistaken, the problem is in this line:
int i = 0, j = n / 2;
But i don't know how to change the code to support even values
I would assume that you meant normal magic square (where the number are restricted to 1,2..n^2)
First of all, it's impposible to construct such magic square for n=2.
2nd, you would need an whole new algorithm for it, which is much more complicated. The problem (constructing magic square for any even number) is solved in this paper and while there isn't any psaudo code there, the implementation from the explenation is quite straightforward (long one though).
the problem is here:
i = 0;
int j = n / 2;
for (int k = 1; k <= nsqr; ++k)
{
matrix[i][j] = k;
i--;
}
look how you decrement i inside the loop and making it as an index of the array so:
matrix[-3][j] = k; // will be in your code
you are messing deliberately with the indexes of the array
I found answer on my question in this artcile
I made full revision my algorithm based on this article. Later posted listing the resulting program

C++ for loop array assignment. Getting junk returned

I'm trying to generate LIMIT (lets say limit = 1000) prime numbers and store them to an array, but I get junk returned. Here's my code:
#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
int primes[1000];
int n, p, t, LIMIT = 1000;
for(n=2; n <= LIMIT; n++)
{
t=0;
for(p=2; p <= n/2; p++)
{
if (n%p == 0)
{
t = 1;
break;
}
}
if(!t)
primes[p-2] = n;
}
for (int i = 0; i < LIMIT; i++)
cout << primes[i] <<" ";
return 0;
}
Define a variable outside the outer loop:
int count=0;
and then use it here:
primes[count++] = n;
then print as:
for (int i = 0; i < count; i++)
cout << primes[i] <<" ";
Explanation:
You're not generating 1000 prime numbers, rather you're generating all prime numbers less than or equal to 1000.
As #Jerry Coffin commented, your code should be like this:
Note : I'm not talking about correctness, rather the skeleton of the program; so you decide if is_prime() function is correct or not, optimized or not, etc.
bool is_prime(int n)
{
for(int p=2; p <= n/2; p++)
{
if (n%p == 0)
{
return false;
}
}
return true;
}
int main()
{
int primes[1000];
int n, p, t, LIMIT = 1000;
int count=0;
for(n=2; n <= LIMIT; n++)
{
if (is_prime(n) )
primes[count++] = n;
}
for (int i = 0; i < count; i++)
cout << primes[i] <<" ";
return 0;
}
Correctness and Optimization of is_prime():
Now you decide the correctness of is_prime(). Is it correctly written? Is it optimized? Do you really need to check for all integers in the range [2,n/2]?