Why is mpz_nextprime returning an even number (54)? - primes

I have to find out the first prime number grater then 4 using library and I get 54 (q2 variable in the code). Any idea why? Could be the malfunction related to the 32 bit processor of the computer? I would be very grateful for your help!
Hereby the code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
void generate_Keys()
{
mpz_t q,q2;
int rndBit;
gmp_randstate_t gmpRandState2;
mpz_init(q);
mpz_init(q2);
rndBit = 4;
gmp_randinit_default(gmpRandState2);
mpz_urandomb(q, gmpRandState2, rndBit);
cout<<" q="<<q;//4
mpz_nextprime(q2,q);
cout<<" nextprime="<<q2;//54
}
int main()
{
generate_Keys();
return 0;
}

Related

Want to print the hexadecimal value of a pointer as 'human readable' unsigned value

This is what I coded on Mobile C
#include <iostream>
using namespace std;
int main()
{
int i{14},j{20};
int *pj{&j},*pi{&i};
cout<<endl<<(unsigned)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned)pj;
cout<<"\t"<<*pj;
--pi;
++pj;
cout<<endl<<(unsigned)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned)pj;
cout<<"\t"<<*pj;
return 0;
}
And this was the output:
2582956840 14
2582956836 20
2582956836 20
2582956840 14
But when I'm trying to do this on visual code (fedora 36) and the terminal compiler g++ is throwing errors, as mentioned above in the Imgur image.
EDIT2:
I did this:
#include <iostream>
using namespace std;
int main()
{
unsigned int i{14},j{20};
unsigned int *pj{&j},*pi{&i};
cout<<endl<<(unsigned*)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned*)pj;
cout<<"\t"<<*pj;
--pi;
++pj;
cout<<endl<<(unsigned*)pi;
cout<<"\t"<<*pi;
cout<<endl<<(unsigned*)pj;
cout<<"\t"<<*pj;
return 0;
}
And got this output:
0x7fff539232d0 14
0x7fff539232d4 20
0x7fff539232cc 21863
0x7fff539232d8 1402090200
Strange! It was compiled using onlinegdb C++ compiler.
You can get the pointers value by doing this:
#include <iostream>
int main() {
int a = 2;
cout < &a;
}
This is pretty much as best you can do to make it human readable.
Also you can look at Displaying the address of a string if you want to look at any other approaches.

Giving the change using a "greedy "method - what(): std::bad_allock

After expanding my program to include change such as 0.01,0.02,0.05,0.1,0.2,0.5 (zł) I was given:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Process returned 3 (0x3) execution time : 56.358 s
Press any key to continue.
It isn't the first time I have gotten this message, but it only happens upon using vectors.
The program would be working fine had I refrained from adding the update, but I'm curious as to why this message pops out, and what the cause of it may be. I suppose it has to do with the bad placement of something in the memory?
Thank you for your help people.
#include <iostream>
#include <vector>
using namespace std;
int main(){
int iloscMonet=9;
double monety[iloscMonet]={0.01,0.02,0.05,0.1,0.2,0.5,1,2,5};
double resztaDoWydania=4.01;
int licznikMonet=0;
vector <int> jakieMonety;
while(resztaDoWydania){
int nominal = 0;
for(int i=0;i<iloscMonet;i++){
if((monety[i]<=resztaDoWydania)&&(monety[i]>nominal)){
nominal=monety[i];
}
}
resztaDoWydania-=nominal;
jakieMonety.push_back(nominal);
licznikMonet++;
}
cout<<"ile monet?: "<<licznikMonet<<endl;
cout<<"jakie monety?: ";
for(int i=0;i<jakieMonety.size();i++){
cout<<jakieMonety.at(i)<<" ";
}
return 0;
}
Calculation of floating-point number may contain errors.
When I add #include <cstdio> in top of your code and printf("%.30f\n", resztaDoWydania); after licznikMonet++;, I found that the value of resztaDoWydania stacking at 0.009999999999999786837179271970.
You should avoid using floating-point numbers as much as you can.
In this case, you can multiply each of the values by 100 to make them integers.
#include <iostream>
#include <vector>
using namespace std;
int main(){
int iloscMonet=9;
int monety[iloscMonet]={1,2,5,10,20,50,100,200,500};
int resztaDoWydania=401;
int licznikMonet=0;
vector <int> jakieMonety;
while(resztaDoWydania){
int nominal = 0;
for(int i=0;i<iloscMonet;i++){
if((monety[i]<=resztaDoWydania)&&(monety[i]>nominal)){
nominal=monety[i];
}
}
resztaDoWydania-=nominal;
jakieMonety.push_back(nominal);
licznikMonet++;
}
cout<<"ile monet?: "<<licznikMonet<<endl;
cout<<"jakie monety?: ";
for(int i=0;i<jakieMonety.size();i++){
cout<<jakieMonety.at(i)<<" ";
// if you want outpuf of floating-point numbers as the original
//cout<<(jakieMonety.at(i)/100.0)<<" ";
}
return 0;
}
try while(resztaDoWydania>0)

C++ Array manipulation

Hey guys this is the question's link from hackerrank
hackerrank problem
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long int sizeArr, operation;
cin>>sizeArr>>operation;
long int array[sizeArr];
for(long int i=0;i<sizeArr;i++)
array[i]=0;
for(long int i=0;i<operation;i++)
{
long int a,b,k;
cin>>a>>b>>k;
for(long int j=a-1;j<=b-1;j++)
array[j]+=k;
}
sort(array,array+sizeArr);
cout<<array[sizeArr-1];
}
I coded it like this and the another person code it like
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,M,a,b,k,i,j,max=0,x=0;
cin>>N>>M;
long int *Arr = new long int[N+1]();
for(i=0;i<M;i++)
{
cin>>a>>b>>k;
Arr[a]+=k;
if((b+1)<=N) Arr[b+1]-=k;
}
for(i=1;i<=N;i++)
{
x=x+Arr[i];
if(max<x) max=x;
}
cout<<max;
return 0;
}
Mine one didn't clear all the test cases but the second one's code did. Any suggestions.
The problem is that your colleague has this if((b+1)<=N) Arr[b+1]-=k; which means he sometimes subtracts but in your code, you only add in here array[j]+=k;.
And also your while function has a bigger range than your colleague (for example if a user gives as a=1 and b=3 you go through 0,1,2 but your colleague only goes at 1 and 3. I am not sure that this is the problem but you should check out.

Why does the program to find the largest number formed using elements of an array not work?

Visit https://www.interviewbit.com/problems/largest-number/ for the question...
Now I wrote the below code to solve the question (although I used an array to store the number, will do the storing in strings part later..)-
So in this algorithm, I basically used quicksort but with a twist, I changed the definition of greater than or lesser than of two numbers say X, Y such that if the number formed by using X first and Y second or XY is >= YX then greater than(X, Y) is true
In the present scenario, the code is giving runtime error, which I can't understand why, also after a bit of debugging as shown in the comments, still the answer is not coming as expected.
#include <iostream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std ;
bool greaterthan(int a,int b)
{
int n1,n2,s1,s2;
n1=((int )log10(a))+1;
n2=((int)log10(b))+1;
s1=a*((int )pow(10,n2))+b;
s2=a + ((int )pow(10,n1))*b;
if(s1>=s2){return true;}
else{return false;}
}
int spartitions(vector<int >&B,int s , int e)
{
int pivot = B[e];
int pin =s;
int i;
for(i=s;i<=e;i++) //if i change this to i<e
{
if(B[pin]>=pivot)
{swap(B[pin],B[i]);
pin++;
}
// and add swap(B[pin],B[e]);
}
return pin-1; // and return pin here then it works but not give correct output
}
int prand(vector<int >&B,int s ,int e)
{
srand(time(NULL));
int n = rand()%(e-s+1)+s;
swap(B[n],B[e]);
int pin = spartitions(B,s,e);
return pin;
}
void qsort(vector<int >&B,int s, int e )
{
if(s<e){
int p= prand(B,s,e);
qsort(B,s,p-1);
qsort(B,p+1,e);
}
}
vector<int> largestnumber(vector<int >&A)
{
int n =A.size();
vector<int >B(n);
B=A;
qsort(B,0,n-1);
return B;
}
int main()
{
int n;
cin>>n;
vector<int>A(n);
int i;
for(i=0;i<n;i++)
{
cin>>A[i];
}
vector<int >B(n);
B=largestnumber(A);
for(i=0;i<n;i++)
{
cout<<B[i];
}
}
Please Help as I am a newbie in programming and can't figure this out from like 3-4 hours ...??
Would really appreciate if someone can correct my code only and not give a different algorithm, as I want this algorithm to be corrected.
Your self-written qsort function recursively calls itself, which adds more things to the stack, which only has so much space. When the list is too big, there will be too many function calls in the stack and it overflows. That's why anything less than 5 for the first input (which is for n) works fine but as soon as you exceed that, you get a runtime error. Consider not using a recursive function call.
Edit: Enabling optimisation also seems to fix this issue.
This may not work depending on the compiler and how it optimises. (Works on MSVC)

For and while loop not working in a globally defined function

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int help(int i,int money,int denomination[]){
int sum=0;
int j=0;
while(i>0){
if(i&1) sum+=denomination[j];
i=i>>1;
j+=1;
}
return sum==money?1:0;
}
int ans(int numOfNotes,int money,int denomination[]){
for(int i=0;i<(1<<numOfNotes);i++){
if(help(i,money,denomination)){
return 1;
}
}
return 0;
}
int main() {
int testCases,numOfNotes,money;
cin>>testCases;
while(testCases>0){
cin>>numOfNotes>>money;
int denomination[numOfNotes];
int i=0;
while(numOfNotes){
cin>>denomination[i];
i++;
numOfNotes--;
}
testCases--;
ans(numOfNotes,money,denomination)==1?cout<<"Yes"<<endl:cout<<"No"<<endl;
}
return 0;
}
If there exists a subset of array denomination such that it amounts to money, program should show "Yes", else "No".
But for the following simple input
1
3 3
1
1
1
output is coming out to be
No
whereas it should be
Yes
According to me, the for and while loops are not working in the ans and help functions. Is there any other bug in the program?
You're modifying numOfNotes in the inner while loop in main, which means its value is 0 when later passed to ans. The loops in the functions are working, you're just giving them other limits than you expected.
You can easily solve issues like this yourself by stepping through your program in a debugger and inspecting variable values and control flow along the way. This is a vital programming skill. See also How to debug small programs by Eric Lippert.
Unrelated to the problem at hand, but please also see these SO questions about what's wrong with including bits/stdc++.h and declaring using namespace std;.