Too much memory being used on low intensity work. c++ - c++

Keep in mind I've only been using CPP for around a week so the code is not very good. I wrote this to solve the problem 1362A on CodeForces. Long story short, the code works fine except for the fact that it says the memory used is 262100 KB. I've looked over it multiple times but can't find anything that would cause lots of memory to be used.
#include <iostream>
#include <cstdint>
using namespace std;
uint64_t makeq(uint64_t x, uint64_t y)
{
uint64_t q;
if (x >= y)
{
q = x / y;
}
else
{
q = y / x;
}
return q;
}
bool check(uint64_t x, uint64_t y, uint64_t q)
{
if (x >= y)
{
if (x % y != 0)
{
cout << -1 << "\n";
return false;
}
}
else
{
if (y % x != 0)
{
cout << -1 << "\n";
return false;
}
}
//checking float
if (q > 8 && q % 8 != 0)
{
cout << -1 << "\n";
return false;
}
if (q == 1)
{
cout << 0 << "\n";
return false;
}
if (q % 2 != 0)
{
cout << -1 << "\n";
return false;
}
return true;
}
int main()
{
uint64_t n, x, y, q, c;
cin >> n;
while (n--)
{
c = 0;
cin >> x >> y;
q = makeq(x, y);
if (check(x, y, q) == false)
{
continue;
}
while (q > 1)
{
if (q >= 8 && q % 8 == 0)
{
q /= 8;
c += 1;
}
else
{
if (q >= 4 && q % 4 == 0)
{
q /= 4;
c += 1;
}
else
{
q /= 2;
c += 1;
}
}
}
cout << c << endl
<< flush;
}
return 0;
}
**

Related

How to solve this problem using recursion?

My function has to be able to print the sum of the numbers 1+1+1.... to get to N. I can only enter N. For example: sumaRecursiva(6): 1+1+1+1+1+1=6.
This is what I have:
int sumaRecursiva(int y) {
int x=0;
if (x == y){
return y;
}
else {
x += 1;
cout << x << "+" <<endl;
sumaRecursiva(y-1);
}
}
You may want another function to handle the fact that you need fewer + characters in your print than you need 1s
void printSum(int y) {
if (y == 0) return;
cout << 1;
printPlusRecursive(y - 1);
}
void printPlusRecursive(int y) {
if (y == 0) return;
cout << '+' << 1;
printPlusRecursive(y - 1);
}
or shorter, but more complex version:
void printSum(int y, bool firstCall = true) {
if (y == 0) return;
if (firstCall) cout << 1;
else cout << '+' << 1;
printSum(y - 1, false);
}
If you want the output to be exactly 1+1+1+1+1+1=6 then you will need a helper function sumaRecursivaHelper to print the 1+'s and the outer function to print the =n which I believe none of the other answers include.
#include <iostream>
using namespace std;
void sumaRecursivaHelper(int x) {
if (x == 0) {
cout << 1;
}
else {
cout << "1+";
sumaRecursivaHelper(x - 1);
}
}
void sumaRecursiva(int x) {
if (x == 0) {
cout << 0;
}
else {
sumaRecursivaHelper(x);
}
cout << "=" << x;
}
int main()
{
sumaRecursiva(6);
return 0;
}
Your recursive function should return a value.
The value passed to a recursive is usually used to test for the escape condition (not usually used as part of the result).
int sum(int N)
{
if (N == 0) {
return 0; // I like zero as the escape value.
// Though this will give you
// sum(6) = 1 + 1 + 1 + 1 + 1 + 1 + 0 = 6
// If you only want to add ones change
// the escape condition to 1 and return 1
// Though this will prevent you doing sum(0)
}
return 1 + sum(N-1);
}

Print prime factorization in exponential form in C++

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.

Issues With Miller-Rabin Test

I'm attempting to debug my implementation of the standard (i.e., probabilistic) Miller-Rabin test. This is the code right now, with a rudimentary test execution in the main method:
#include <iostream>
#include <random>
typedef unsigned long long int ulli;
std::random_device rd;
std::mt19937_64 mt(rd());
ulli upow(ulli x, ulli y) {
if (y == 0)
return 1;
else if (y == 1)
return x;
else if (y % 2 == 0)
return upow(x*x, y/2);
else
return x * upow(x*x, (y-1)/2);
}
bool miller_rabin(ulli n, ulli d) {
std::uniform_int_distribution<ulli> dist(2, n-2);
ulli a = dist(mt);
ulli x = upow(a, d) % n;
if (x == 1 || x == n - 1)
return true;
while (d != n - 1) {
x = x*x % n;
d *= 2;
if (x == 1)
return false;
if (x == n - 1)
return true;
}
return false;
}
bool is_prime(ulli n, ulli k) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0)
return false;
ulli d = n - 1;
while (d % 2 == 0)
d /= 2;
for (int i = 0; i < k; ++i) {
if (!miller_rabin(n, d))
return false;
}
return true;
}
int main() {
ulli tests = 30;
std::cout << "is_prime(2): " << is_prime(2, tests) << std::endl;
std::cout << "is_prime(10): " << is_prime(10, tests) << std::endl;
std::cout << "is_prime(97): " << is_prime(97, tests) << std::endl;
std::cout << "is_prime(256): " << is_prime(256, tests) << std::endl;
std::cout << "is_prime(179424691): " << is_prime(179424691, tests) << std::endl;
std::cout << "is_prime(32416187563): " << is_prime(32416187563, tests) << std::endl;
}
The problem I'm currently having is this: the last two tests are returning false, which (because they are both prime) should be impossible. Even with as high a number of tests as I'm passing, is_prime still outputs false.
At this point, I'm genuinely lost as to what's wrong. Any ideas?

Recurrence in Math

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;
}

User defined function returning wrong count in c++

I'm trying to write program to calculate and display the number of primes in the first 50 “chiliads”. There must be 2 user defined functions "isPrime" and "primeCount". It seems like the "primeCount" is appending 4469969 to every count. It isn't doing that when I paste it into the main function.
int main (){
long x = 1;
long y = 1000;
char reply;
cout << "Start End Number of Primes" << endl;
while (y <= 50000)
{
cout << x << " " << y << " ";
cout << primeCount(x, y) << endl;
x = 1000 + x;
y = 1000 + y;
}
//exits the program
cout << "Enter q to quit... ";
cin >> reply;
return 0;
}// End main function
bool isPrime(long n)
{
long i = 2;
if (n == 1)
return false;
if (n == 2)
return true;
if (n == 3)
return true;
if ((n % 2) == 0)
return false;
if ((n % 3) == 0)
return false;
while (i < n)
{
if (n % i == 0 )
{
return false;
}
else
{
i++;
}
}
return true;
}
long primeCount (long x, long y)
{
long count = 0;
while (x < y)
{
if (isPrime(x) == 1)
{
count++;
}
x++;
}
cout << count;
}
You were not returning a value from "primeCount" you were printing it.
I cleaned up the code as follows, along with some optimizations (we prove that the candidate is odd, so we don't need to check even divisors, and we only check for the value of 2 when we already know the number is even, saving us 1 extra test per odd number).
#include <iostream>
// prototypes for functions we implement after we use them.
long primeCount(long x, long y);
bool isPrime(long n);
int main (){
long x = 1;
long y = 1000;
std::cout << "Start End Number of Primes" << std::endl;
while (y <= 50000)
{
std::cout << x << " " << y << " ";
std::cout << primeCount(x, y) << std::endl;
x += 1000;
y += 1000;
}
return 0;
}
bool isPrime(long n)
{
if((n & 1) == 0) // even
return (n == 2);
if(n == 1)
return false;
for (long i = 3; i < n / 2; i += 2)
{
if ((n % i) == 0 )
return false;
}
return true;
}
long primeCount(long x, long y)
{
long count = 0;
while (x < y)
{
if (isPrime(x))
count++;
++x;
}
return count;
}