I had to create a function that takes an integer parameter called number and the function would check to see if number is a prime number or not. Everything works but the output to the user would be a either 0 or 1, which is false and true, respectively. But how can I change that ..

#### Category : primes

In my textbook, the algorithms and flowcharts for this problem use flag variables.So I gave it a try without using flag variables and got a solution (given below).But its implementation in C++ is quite puzzling. We haven’t officially learnt goto statements yet. So my doubt is whether its implementation in C++ is possible without using ..

My goal is that: the program return the list of numbers in the vector i made, which are the primes numbers, the code is wrong, #include <iostream> #include <vector> #include <math.h> using namespace std; vector < int > L; bool mark[10]; void colador() { fill(mark,mark+10, false); for(int i=2; i*i <=10; i++) { if(!mark[i]) { L.push_back(i); ..

I am interested in how you can generate an array of prime numbers at compile time (I believe that the only way is using metaprogramming (in C++, not sure how this works in other languages)). Quick note, I don’t want to just say int primes[x] = {2, 3, 5, 7, 11, …};, since I want ..

My code is working fine for small input but for say n=50, its printing wrong answer. For n=50, its printing 70436 instead of printing 265470434. Help me debug the code. Also, I think the issue can be with modulo operatiion. #include <iostream> #include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 ll ..

I’ve been typing away at my keyboard trying to edit my prime finding code so it is not all primes, just some of a certain equation. Here is my C++ code: #include <iostream> using namespace std; int main() { int low, high, i; bool isPrime = true; cout << "Enter two numbers (intervals): "; cin ..

I have been trying to solve this problem. I think that for n>11, this is not possible, since the diagonal of the square is smaller than the number of primes that fit: n^2 < p < (n^2)/2. but I need to make optimizations since the code will only work for n < 5. The code ..

Here is my code where I wanted to check if a number is prime or not? But it showing the right answer for some numbers and also showing the wrong answer for some numbers. What is the mistake here ?? #include <iostream> using namespace std; int prime(int a); int main(){ int number {}; cout << ..

// how this function checks if a number is prime or not bool isPrime(ll n){ if(n == 2LL) return 1; if(n%2LL == 0LL) return 0; for(ll i=3;i*i<=n;i++){ if(n%i==0); return 0; } return 1; } Source: Windows Que..

I have the following function written in C++ to check whether a number is a prime: bool isPrime(ll n){ if(n == 2LL) return 1; if (n % 2LL == 0LL) return 0; for(ll i=3; i*i <= n; i++) { if (n % i == 0); return 0; } return 1; } How does this code ..

## Recent Comments