I wrote this to find out which 10-digit numbers are prime but it stops after showing about 20 numbers at all. Would you help me figure out what's wrong? Or suggesting any other sources? It's important to me as it's my school project.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void temp()
{
static long long n1=9999999999 ;
long long n2,n3;
if (n1 <= 1 || n1%2==0|| n1%3==0|| n1%5==0|| n1%7==0|| n1%11==0|| n1%13==0|| n1%17==0|| n1%19==0|| n1%23==0|| n1%29==0|| n1%31==0|| n1%37==0|| n1%41==0|| n1%43==0|| n1%47==0|| n1%53==0|| n1%59==0|| n1%61==0|| n1%67==0|| n1%71==0|| n1%73==0|| n1%79==0|| n1%83==0|| n1%89==0|| n1%97==0)
{
std::cout<< n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2 = (n1 - 1)/2;
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout<< n1 << " not prime\n\n";
}
else
{
n2--;
}
}
std::cout<< n1 << " prime\n\n";
n1--;
temp();
}
}
int main(int argc, char** argv)
{
temp();
return 0;
}
When n1 is 9999999983 it gets stuck inside the while loop until n2 is less or equal to 1
You should look for a more efficient way
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout << n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2--;
}
}
EDIT:
This is what you could do in your case (Found it here and changed it to fit your needs (I found it here and changed it up a bit to fit your needs)
#include <iostream>
using namespace std;
void check() {
int n = 9999999999;
while (n != 0) {
int i, m = 0, flag = 0;
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
cout << "Number is not Prime." << endl;
flag = 1;
break;
}
}
if (flag == 0)
cout << "Number is Prime." << endl;
n--;
}
}
int main()
{
check();
return 0;
}
Related
For each number read from the standard input the program should print YES if it is a Tribonacci number and NO otherwise. What am I doing wrong in my program, it prints YES, but it wont print NO when the number is not a tribonacci number. For example when number is 45, it should print no.
Tribonacci number formula
T0=0
T1=1
T2=2
Tn=Tn-1 + Tn-2 + Tn-3 (for n>2)
using namespace std;
bool isTrib(int n) {
if(n==0 || n == 1 || n == 2) {
return true;
}
if(n > 2) {
int trib = isTrib(n-1)+isTrib(n-2)+isTrib(n-3);
if(trib == n) {
return true;
}
}
return false;
}
int main()
{
int n;
while(cin>>n) {
bool result = isTrib(n);
cout << result;
result == 1 ? cout << "YES" << endl : cout << "NO" << endl;
}
return 0;
}
you're mixing two things: actual tribonacci numbers and "true"/"false" answer to question "whether N is tribonacci", for example variable trib in your code can be either 0, 1, 2 or 3, it cannot take any other values, but you're trying to compare it with real number, which is apples to oranges
here is fixed version:
bool isTrib(int n) {
if(n==0 || n == 1 || n == 2) {
return true;
}
int n1 = 0;
int n2 = 1;
int n3 = 2;
int trib = n1 + n2 + n3;
while (trib <= n) {
if (trib == n) return true;
n1 = n2;
n2 = n3;
n3 = trib;
trib = n1 + n2 + n3;
}
return false;
}
int main()
{
int n;
while(cin>>n) {
bool result = isTrib(n);
cout << (result ? "YES" : "NO") << endl;
}
return 0;
}
In the UVA problem "100 The 3n + 1 problem" when i submit the code it says runtime error. The code is in C++
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36
I tried many things but i still cannot solve that problem
#include <iostream>
#include <algorithm>
#define ll long long
#define N 10000
using namespace std;
int main(){
int cont = 0, aux = 0;
ll n1, n2, m;
ll dp[N] = {0};
while (cin >> n1 >> n2) {
cout << n1 << " " << n2 << " ";
if(n1 > n2)
{
m = n2;
n2 = n1;
n1 = m;
}
for (ll i = n1; i <= n2; i++) {
ll num = i;
if (num == 1) {
cont = 1;
}
else{
if (dp[num] != 0){
cont = dp[num];
}
else {
while (num >= 1){
if (num == 1) {
cont += 1;
break;
}
if (num%2 != 0){
num = (3 * num) + 1;
}
else{
num = num/2;
}
cont += 1;
}
dp[num] = cont;
}
}
aux = max(aux, cont);
cont = 0;
}
cout << aux << "\n";
aux = 0;
}
}
The limit on input is 1,000,000 (not 10,000). The limit is stated wrong.
Similar problem:
https://www.spoj.com/problems/PROBTNPO/
I have some simple code in C++ (see below), that works good and can be successfully debugged in Microsoft Visual Studio.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <stdlib.h>
#include <iomanip>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
Rational() {
numerator = 0;
denominator = 1;
};
Rational(int p, int q) {
int divider = find_common_divider(abs(p), abs(q));
p = p / divider;
q = q / divider;
if ((p < 0) && (q < 0)) {
p = abs(p);
q = abs(q);
}
else if ((p > 0) && (q < 0)) {
p = -p;
q = abs(q);
}
if (p == 0)
q = 1;
numerator = p;
denominator = q;
};
int Numerator() const {
return numerator;
};
int Denominator() const {
return denominator;
};
int find_common_divider(int N1, int N2) {
while ((N1 > 0) && (N2 > 0)) {
if (N1 > N2)
N1 %= N2;
else
N2 %= N1;
}
return N1 + N2;
};
};
int main() {
{
const Rational r(3, 10);
if (r.Numerator() != 3 || r.Denominator() != 10) {
cout << "Rational(3, 10) != 3/10" << endl;
return 1;
}
}
{
const Rational r(8, 12);
if (r.Numerator() != 2 || r.Denominator() != 3) {
cout << "Rational(8, 12) != 2/3" << endl;
return 2;
}
}
{
const Rational r(-4, 6);
if (r.Numerator() != -2 || r.Denominator() != 3) {
cout << "Rational(-4, 6) != -2/3" << endl;
return 3;
}
}
{
const Rational r(4, -6);
if (r.Numerator() != -2 || r.Denominator() != 3) {
cout << "Rational(4, -6) != -2/3" << endl;
return 3;
}
}
{
const Rational r(0, 15);
if (r.Numerator() != 0 || r.Denominator() != 1) {
cout << "Rational(0, 15) != 0/1" << endl;
return 4;
}
}
{
const Rational defaultConstructed;
if (defaultConstructed.Numerator() != 0 || defaultConstructed.Denominator() != 1) {
cout << "Rational() != 0/1" << endl;
return 5;
}
}
cout << "OK" << endl;
return 0;
}
But when I start debugging in VSCode (UBUNTU 18.04) from line
const Rational r(3, 10) in main() with "Step into" up to line
int divider = find_common_divider(abs(p), abs(q));
debugger gives exception "Unable to open 'abs.c': Unable to read file '/build/glibc-OTsEL5/glibc-2.27/stdlib/abs.c' (Error: Unable to resolve non-existing file '/build/glibc-OTsEL5/glibc-2.27/stdlib/abs.c')."
That exception is absent in Microsoft Visual Studio and debugger goes to function "find_common_divider".
What is wrong with my VSCode ?
There is nothing wrong with your visual studio code, you simply don't have the source code for abs so can't step into it. Ignore the error, hit step out to get back to your code.
So far I have this code. I'm trying to print prime factorization with exponents. For example, if my input is 20, the output should be 2^2, 5
#include <iostream>
#include <cmath>
using namespace std;
void get_divisors (int n);
bool prime( int n);
int main(int argc, char** argv) {
int n = 0 ;
cout << "Enter a number and press Enter: ";
cin >>n;
cout << " Number n is " << n << endl;
get_divisors(n);
cout << endl;
return 0;
}
void get_divisors(int n){
double sqrt_of_n = sqrt(n);
for (int i =2; i <= sqrt_of_n; ++i){
if (prime (i)){
if (n % i == 0){
cout << i << ", ";
get_divisors(n / i);
return;
}
}
}
cout << n;
}
bool prime (int n){
double sqrt_of_n = sqrt (n);
for (int i = 2; i <= sqrt_of_n; ++i){
if ( n % i == 0) return 0;
}
return 1;
}
I hope someone can help me with this.
You can use an std::unordered_map<int, int> to store two numbers (x and n for x^n). Basically, factorize the number normally by looping through prime numbers smaller than the number itself, dividing the number by the each prime as many times as possible, and recording each prime you divide by. Each time you divide by a prime number p, increment the counter at map[p].
I've put together a sample implementation, from some old code I had. It asks for a number and factorizes it, displaying everything in x^n.
#include <iostream>
#include <unordered_map>
#include <cmath>
bool isPrime(const int& x) {
if (x < 3 || x % 2 == 0) {
return x == 2;
} else {
for (int i = 3; i < (int) (std::pow(x, 0.5) + 2); i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
std::unordered_map<int, int> prime_factorize(const int &x) {
int currentX = abs(x);
if (isPrime(currentX) || currentX < 4) {
return {{currentX, 1}};
}
std::unordered_map<int, int> primeFactors = {};
while (currentX % 2 == 0) {
if (primeFactors.find(2) != primeFactors.end()) {
primeFactors[2]++;
} else {
primeFactors[2] = 1;
}
currentX /= 2;
}
for (int i = 3; i <= currentX; i += 2) {
if (isPrime(i)) {
while (currentX % i == 0) {
if (primeFactors.find(i) != primeFactors.end()) {
primeFactors[i]++;
} else {
primeFactors[i] = 1;
}
currentX /= i;
}
}
}
return primeFactors;
}
int main() {
int x;
std::cout << "Enter a number: ";
std::cin >> x;
auto factors = prime_factorize(x);
std::cout << x << " = ";
for (auto p : factors) {
std::cout << "(" << p.first << " ^ " << p.second << ")";
}
}
Sample output:
Enter a number: 1238
1238 = (619 ^ 1)(2 ^ 1)
To begin with, avoid using namespace std at the top of your program. Second, don't use function declarations when you can put your definitions before the use of those functions (but this may be a matter of preference).
When finding primes, I'd divide the number by 2, then by 3, and so on. I can also try with 4, but I'll never be able to divide by 4 if 2 was a divisor, so non primes are automatically skipped.
This is a possible solution:
#include <iostream>
int main(void)
{
int n = 3 * 5 * 5 * 262417;
bool first = true;
int i = 2;
int count = 0;
while (i > 1) {
if (n % i == 0) {
n /= i;
++count;
}
else {
if (count > 0) {
if (!first)
std::cout << ", ";
std::cout << i;
if (count > 1)
std::cout << "^" << count;
first = false;
count = 0;
}
i++;
if (i * i > n)
i = n;
}
}
std::cout << "\n";
return 0;
}
Note the i * i > n which is an alternative to the sqrt() you are using.
Sorry this is my first time use stackoverflow.
I dont kow where is the mistake in my code.
Output that i want:
-1+3-5+7-9+11-13+15
RESULT : 8
But Output that is shown
-1+3-5+7-9+11-13+15
RESULT : 10
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i, S, x, sign;
S = 0;
for (i = 1; i <= 8; i++) {
if ((pow(-1, i - 1) == 1) && (i > 1)) {
sign = -1;
}
if ((pow(-1, i - 1) != 1) && (i > 1)) {
sign = 1;
cout << "+";
}
if (i == 1) {
sign = 1;
cout << "-";
}
x = sign * (2 * i - 1);
cout << x;
S = S + x;
}
cout << "\n Result:" << S;
}
problem is in the if condition block where you check i==1
in that loop you are making sign=1 that should be sign=-1
How about improving the logic like following?
#include <iostream>
using namespace std;
int main()
{
int i;
bool sign = true; // signed/minus = true, non-signed/plus = false
int ans = 0;
for( i=1; i<=15; i=i+2){
if( sign == true){
cout << "-" << i;
ans = ans - i;
}
else {
cout << "+" << i;
ans = ans + i;
}
sign = !sign;
}
cout << endl << "RESULT : " << ans << endl;
return 0;
}
Try this code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i, S, x, sign;
S = 0;
for (i = 1; i <= 8; i++) {
if ((pow(-1, i - 1) == 1) && (i > 1)) {
sign = -1;
}
else
if ((pow(-1, i - 1) != 1) && (i > 1)) {
sign = 1;
// cout << "+";
}
//else
if (i == 1) {
sign = -1;
//cout << "-";
}
x = sign * (2 * i - 1);
cout <<"\n"<<x;
S = S + x;
//cout<<"S is \n"<<S;
}
cout << "\n Result:" << S;
}
You have put wrong sign when i==1
The problem is that you're starting the calculation with a positive sign (but you're lying to yourself by printing "-").
You can simplify the code and don't need to mess around with pow if you make the obervation that
pow(-1, k) == -1 * pow(-1, k-1)
Starting at pow(-1,0) (that is, 1), you can write:
int main(int argc, char* argv[])
{
int sign = 1; // sign will always hold pow(-1, i).
int sum = 0;
for (int i = 1; i <= 8; i++)
{
sign *= -1;
if (sign > 0) // Since sign starts at -1, we know that i > 1 here
{
std::cout << "+";
}
int term = sign * (2 * i - 1);
std::cout << term;
sum += term;
}
std::cout << " = " << sum << std::endl;
}