Dec2Bin conversion

Z

zacariaz

This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)

@code
#include <vector>
#include <iostream>

class T {

std::vector<bool>B;

public:

void Convert(unsigned long a) {
B.clear(); //just in case
for(int i = 0; i < 32; i++, a = a >> 1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B != 1; i--) {
B.pop_back();
}
}

void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B;
}
}

} test;

int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code

Your input will be appreciated.

Regards
 
B

BobR

This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)

@code
#include <vector>
#include <iostream>

class T {
std::vector<bool>B;
public:
void Convert(unsigned long a) {
B.clear(); file://just in case
for(int i = 0; i < 32; i++, a = a >> 1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B != 1; i--) {
B.pop_back();
}
}

void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B;
}
}

} test;

int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code

Your input will be appreciated.
Regards


#include <bitset>
int main(){
std::bitset<64> b;
b=255;
std::cout<<"b=255;\n"<< b <<std::endl;
return 0;
}

Also check out 'std::bit_vector'.
 
Z

zacariaz

This is pretty much beginners stuff, but anyway i though i would ask
the question.
Does it have to be this complicated?
I think the below code speaks for itself, but basicly i need to
convert an unsigned integer into binary and stuff it into a boolean
vector. (supose an aray could do allso, but this is easyer)
@code
#include <vector>
#include <iostream>
class T {
std::vector<bool>B;
public:
void Convert(unsigned long a) {
B.clear(); file://just in case
for(int i = 0; i < 32; i++, a = a >> 1) {
B.push_back(a & 1);
}
for(int i = 31; i >= 0 && B != 1; i--) {
B.pop_back();
}
}

void Print() {
for(int i = B.size() - 1; i >= 0; i--) {
std::cout << B;
}
}

int main() {
test.Convert(255);
test.Print();
std::cin.get();
}
@code
Your input will be appreciated.
Regards

#include <bitset>
int main(){
std::bitset<64> b;
b=255;
std::cout<<"b=255;\n"<< b <<std::endl;
return 0;
}

Also check out 'std::bit_vector'.

--
Bob R
POVrookie- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -


bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.

I actually thought that vector<bool> was more or less the same as
bit_vector, except ive heard that i should use vector<bool>
 
B

BobR

bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.
I actually thought that vector<bool> was more or less the same as
bit_vector, except ive heard that i should use vector<bool>

[ My STL docs are a little old, so, take this with a grain of salt. <G> ]
"A bit_vector is essentially a vector<bool>: it is a Sequence that has the
same interface as vector. The main difference is that bit_vector is
optimized for space efficiency. ** A vector always requires at least one
byte per element, but a bit_vector only requires one bit per element. ** "

So, if you want to save some memory, use the 'std::bit_vector'.

OK, another little nitpik. You use the 'magic numbers' 32 and 31 in your
loops. On your machine an 'int' (or long) may be 32 bits, BUT, the standard
does NOT require that (it could be 16bits or 64bits). You should use 'sizeof
int' to get the size on the machine it is being compiled on.

std::size_t IntSize( sizeof(int) * CHAR_BIT );
cout <<"sizeof(int) = "<<IntSize<<std::endl;
// out: sizeof(int) = 32

I know you asked how to simplify, so being the jerk, I added complexity!
Ain't I a stinker? <G>

Anyway, play with that, then re-post your new code and I (or one of the
experts here) will see if it can be improved.
The only thing I see at this point is a style issue(mine):

void Print() {
for( std::size_t i( B.size()-1 ); i >= 0; --i ){
std::cout << B;
// std::cout << B.at( i ); // if you want range check (exception)
} // Print()

If you do it like this, you could output to file, stringstream, etc.:

void Print( std::eek:stream &out) {
for( std::size_t i( B.size() - 1 ); i >= 0; --i ){
out << B.at( i );
} // Print()

int main(){
test.Convert(255);
test.Print( std::cout );
std::eek:fstream nuts("test.txt");
test.Print( nuts );
return 0;
}
 
Z

zacariaz

bitset is not an option as it is limited to whatever number of bits
you choose to assign. It needs to be dynamicly.
I actually thought that vector<bool> was more or less the same as
bit_vector, except ive heard that i should use vector<bool>

[ My STL docs are a little old, so, take this with a grain of salt. <G> ]
"A bit_vector is essentially a vector<bool>: it is a Sequence that has the
same interface as vector. The main difference is that bit_vector is
optimized for space efficiency. ** A vector always requires at least one
byte per element, but a bit_vector only requires one bit per element. ** "

So, if you want to save some memory, use the 'std::bit_vector'.

OK, another little nitpik. You use the 'magic numbers' 32 and 31 in your
loops. On your machine an 'int' (or long) may be 32 bits, BUT, the standard
does NOT require that (it could be 16bits or 64bits). You should use 'sizeof
int' to get the size on the machine it is being compiled on.

std::size_t IntSize( sizeof(int) * CHAR_BIT );
cout <<"sizeof(int) = "<<IntSize<<std::endl;
// out: sizeof(int) = 32

I know you asked how to simplify, so being the jerk, I added complexity!
Ain't I a stinker? <G>

Anyway, play with that, then re-post your new code and I (or one of the
experts here) will see if it can be improved.
The only thing I see at this point is a style issue(mine):

void Print() {
for( std::size_t i( B.size()-1 ); i >= 0; --i ){
std::cout << B;
// std::cout << B.at( i ); // if you want range check (exception)
} // Print()

If you do it like this, you could output to file, stringstream, etc.:

void Print( std::eek:stream &out) {
for( std::size_t i( B.size() - 1 ); i >= 0; --i ){
out << B.at( i );
} // Print()

int main(){
test.Convert(255);
test.Print( std::cout );
std::eek:fstream nuts("test.txt");
test.Print( nuts );
return 0;
}


Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<bool> only take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.

the code as it looks now (havent played with the bit_vector yet):
@code
class T {
std::vector< std::vector<bool> >B;
public:
void Clear() {
B.clear();
}
void Conv(unsigned long a) {
std::vector<bool>c;
for(int i = 0; i < 32; i++, a = a >> 1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
std::cout << B[a];
}
else
std::cout << "ERROR: element does not exist!";
}
};
@code

If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.
The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).

i have some trouble explaining my self in english, but i hope im doing
ok.
 
B

BobR

Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<bool> only take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.

the code as it looks now (havent played with the bit_vector yet):
@code
class T { [snip, see below ] };
@code

If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.

If you don't know x's size (or can't calculate it), how do you plan to pass
it to the method?
T::Calc(std::bit_vector const &Bvec){/* parse it */} // ??

Maybe you could explain a little more what you are trying to solve.
Explain exactly what you plan to input, and what you want for output.

The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).

i have some trouble explaining my self in english, but i hope im doing
ok.

You are doing fine. Even some educated 'native English speaking' people
sometimes have trouble explaining things. <G> (not me. I ain't educated!).

I just did a quick test using bit_vector. I changed these:

class T {
// std::vector< std::vector<bool> >B;
std::vector< std::bit_vector >B; // <--- here
public:
void Clear(){ B.clear();}
void Conv(unsigned long a) {
// std::vector<bool>c;
std::bit_vector c; // <--- here (2)
for(int i = 0; i < 32; i++, a = a >> 1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a, std::eek:stream &out) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
// std::cout << B[a];
out<<B[a];
}
else // std::cout << "ERROR: element does not exist!";
out<<"ERROR: element does not exist!";
}
};

int main(){
T Ttest;
Ttest.Conv(255);
Ttest.Print(0, std::cout);
}
// out: truetruetruetruetruetruetruetrue

So, 'std::bit_vector' works here with only the two vector changes (the
ostream ref was for my TestBench program (I use stringstreams to simulate
std::cout)(just use your Print() with no change)).

Maybe this will give you an idea:
// #includes: <string>, <iostream>, <sstream>, <bitset>, <vector>
int main(){
using std::cout; // for NG post
// std::bitset< 512 > BitsBig( 255 ); // compiles for me

std::vector<std::bitset< sizeof(unsigned long) * CHAR_BIT > > Vbits;
std::bitset< sizeof(unsigned long) * CHAR_BIT > Bits( 255 );
Vbits.push_back(Bits);
cout <<"Vbits.at(0) = "<<Vbits.at(0)<<std::endl;
cout <<"Vbits.at(0).to_ulong() = "
<<Vbits.at(0).to_ulong()<<std::endl;

std::eek:stringstream GetBits;
GetBits<<Vbits.at(0);
cout <<"GetBits.str() = "<<GetBits.str()<<std::endl;
std::string Sbits( GetBits.str() );
while( Sbits.at(0) == '0'){ Sbits.erase(0, 1);}
cout <<"Sbits = "<<Sbits<<std::endl;
cout<<std::endl;
return 0;
} // main()
/* - output - :
Vbits.at(0) = 00000000000000000000000011111111
Vbits.at(0).to_ulong() = 255
GetBits.str() = 00000000000000000000000011111111
Sbits = 11111111
*/
 
Z

zacariaz

On 14 Maj, 20:47, "BobR" <[email protected]> wrote:
Thanks for the feedback, aspecial about the vector, as people keep
telling me that vector<bool> only take up one bit of memory per
element even though it is my experience that it takes up one byte of
memory.
Next i would propably be on its place to mention that any input number
(for now) wont be greater than 32/64 bit, but the out but can be
unlimited. The conversion in it self is not so important, i just dont
want to write a whole lot of code to do it, if there is a simple way.
the code as it looks now (havent played with the bit_vector yet):
@code
class T { [snip, see below ] };
@code
If we assume this is as it should be, my next task will be a little
more complicated.
x = input value
2^x is what i need to calculate and of course the result will be so
great that i wont be able to store it in a 32/64 integer and thats the
reason for the weird way of doing thing.

If you don't know x's size (or can't calculate it), how do you plan to pass
it to the method?
T::Calc(std::bit_vector const &Bvec){/* parse it */} // ??

Maybe you could explain a little more what you are trying to solve.
Explain exactly what you plan to input, and what you want for output.
The method im thinking about using is: "left-to-right binary
exponentiation" (http://primes.utm.edu/glossary/page.php?
sort=BinaryExponentiation).
i have some trouble explaining my self in english, but i hope im doing
ok.

You are doing fine. Even some educated 'native English speaking' people
sometimes have trouble explaining things. <G> (not me. I ain't educated!).

I just did a quick test using bit_vector. I changed these:

class T {
// std::vector< std::vector<bool> >B;
std::vector< std::bit_vector >B; // <--- here
public:
void Clear(){ B.clear();}
void Conv(unsigned long a) {
// std::vector<bool>c;
std::bit_vector c; // <--- here (2)
for(int i = 0; i < 32; i++, a = a >> 1)
c.push_back(a & 1);
for(int i = 31; i >= 0 && c != 1; i--)
c.pop_back();
B.push_back(c);
}
void Print(unsigned long a, std::eek:stream &out) {
if(a < B.size()) {
for(int i = B[a].size() - 1; i >= 0; i--)
// std::cout << B[a];
out<<B[a];
}
else // std::cout << "ERROR: element does not exist!";
out<<"ERROR: element does not exist!";
}
};

int main(){
T Ttest;
Ttest.Conv(255);
Ttest.Print(0, std::cout);
}
// out: truetruetruetruetruetruetruetrue

So, 'std::bit_vector' works here with only the two vector changes (the
ostream ref was for my TestBench program (I use stringstreams to simulate
std::cout)(just use your Print() with no change)).

Maybe this will give you an idea:
// #includes: <string>, <iostream>, <sstream>, <bitset>, <vector>
int main(){
using std::cout; // for NG post
// std::bitset< 512 > BitsBig( 255 ); // compiles for me

std::vector<std::bitset< sizeof(unsigned long) * CHAR_BIT > > Vbits;
std::bitset< sizeof(unsigned long) * CHAR_BIT > Bits( 255 );
Vbits.push_back(Bits);
cout <<"Vbits.at(0) = "<<Vbits.at(0)<<std::endl;
cout <<"Vbits.at(0).to_ulong() = "
<<Vbits.at(0).to_ulong()<<std::endl;

std::eek:stringstream GetBits;
GetBits<<Vbits.at(0);
cout <<"GetBits.str() = "<<GetBits.str()<<std::endl;
std::string Sbits( GetBits.str() );
while( Sbits.at(0) == '0'){ Sbits.erase(0, 1);}
cout <<"Sbits = "<<Sbits<<std::endl;
cout<<std::endl;
return 0;
} // main()
/* - output - :
Vbits.at(0) = 00000000000000000000000011111111
Vbits.at(0).to_ulong() = 255
GetBits.str() = 00000000000000000000000011111111
Sbits = 11111111
*/

--
Bob R
POVrookie- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -


i did some research on the bit_vector http://www.sgi.com/tech/stl/bit_vector.html
and it seems to me that it best not to use it?
anyway ive done the next sted of my code and made an adder:

@code
std::vector<bool> Add(std::vector<bool>B1, std::vector<bool>B2) {
std::vector<bool>a;
for(unsigned long i = 1; i > 0;) { // im not sure if this is
nessacery.
if(B1.size() > B2.size())
B2.push_back(0);
else if(B1.size() < B2.size())
B1.push_back(0);
else
i = 0;
}
unsigned long carry = 0;
for(unsigned long i = 0; i < B1.size(); i++) { // i am sure this can
be done better.
a.push_back(((B1 ^ B2) ^ carry) | ((B1 & B2) &
carry));
carry = (B1 & B2) | (B1 & carry) | (B2 & carry);
}
if(carry == 1)
a.push_back(carry);
return a;
}
@code
 
B

BobR

/* """
i did some research on the bit_vector
http://www.sgi.com/tech/stl/bit_vector.html
and it seems to me that it best not to use it?
""" */

If you made that decision because you saw the 'deprecated' in the docs, that
was 7 years ago and it's still in all/most implementations, AFAIK. But, the
decision is yours.

/* """
anyway ive done the next sted of my code and made an adder:
@code
std::vector<bool> Add(std::vector<bool>B1, std::vector<bool>B2) {
std::vector<bool>a;

// -----
for(unsigned long i = 1; i > 0;) { // im not sure if this is nessacery.
if(B1.size() > B2.size())
B2.push_back(0);
else if(B1.size() < B2.size())
B1.push_back(0);
else
i = 0;
}
// -----
""" */

Looks like you want to make the two vectors the same size.
Another way:
{ using std::cout // for NG post
std::vector<bool> vecA(5, true);
std::vector<bool> vecB;
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() > vecB.size()){ vecB.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() < vecB.size()){ vecA.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
// - reverse the sizes -
std::vector<bool> vecC;
std::vector<bool> vecD(5, true);
while( vecC.size() > vecD.size()){ vecD.push_back(0);}
while( vecC.size() < vecD.size()){ vecC.push_back(0);}
cout<<"vecC.size()="<<vecC.size()
<<" vecD.size()="<<vecD.size()<<std::endl;
/* - output -
vecA.size()=5 vecB.size()=0
vecA.size()=5 vecB.size()=5
vecA.size()=5 vecB.size()=5
vecC.size()=5 vecD.size()=5
*/
}

Only one of the two while() loops (I showed two examples above) will
execute, the other will 'fall through'. If the vectors are the same size,
*both* while() loop conditions will be false, and thus 'fall through'(not
execute).
The *two* while()s, one after the other, look a little strange, but better
than a chain of if()/else, and it pretty much is self commenting code (any
programmer can easily see what it's doing.). Try it, I think you'll like it.
<G>


/* """
unsigned long carry = 0;
for(unsigned long i = 0; i < B1.size(); i++) { // i am sure this can
be done better.
a.push_back(((B1 ^ B2) ^ carry) | ((B1 & B2) &
carry));
carry = (B1 & B2) | (B1 & carry) | (B2 & carry);
}
if(carry == 1)
a.push_back(carry);
return a;
}
@code
""" */

If only you could use a 'bitset', it would be TOO easy. <G>

Another thing to consider, you pass the two vectors 'by value'. That means a
copy of each (not too much concern if the size is small). I'd pass them by
'const reference':

std::vector<bool> Add( std::vector<bool>const &B1,
std::vector<bool>const &B2) { /* .... */ }

*BUT*, that won't work because you need to change the vectors inside the
function. You would need to adjust the size before passing them to Add().
Just thought I'd mention that as it's good to think about these things when
designing a class (not as an after thought).

If it needs to be done more than once, I'd make a separate 'private:' helper
function to adjust the sizes:
// in class
private:
bool AdjustSize( std::vector<bool> &B1,
std::vector<bool> &B2 ) /*const*/{
if( B1.empty() && B2.empty() ){
return false; // failure, both empty
} // if()
while( B1.size() > B2.size() ){ B2.push_back(0);}
while( B1.size() < B2.size() ){ B1.push_back(0);}
return true;
} // AdjustSize( vector&, vector&)

std::vector<bool> Add( std::vector<bool> B1,
std::vector<bool> B2){
if( not AdjustSize( B1, B2) ){ throw std::terminate();} // or exit(1);
/* .... */
} // Add(vector, vector)


I'm still a little 'foggy' as to what you are trying to solve overall
exactly. <G>
Is this homework, book excersize, hobby project, <choke>work?

[ corrections, as always, are welcome. ]
 
Z

zacariaz

/* """
i did some research on the bit_vectorhttp://www.sgi.com/tech/stl/bit_vector.html
and it seems to me that it best not to use it?
""" */

If you made that decision because you saw the 'deprecated' in the docs, that
was 7 years ago and it's still in all/most implementations, AFAIK. But, the
decision is yours.

/* """
anyway ive done the next sted of my code and made an adder:
@code
std::vector<bool> Add(std::vector<bool>B1, std::vector<bool>B2) {
std::vector<bool>a;

// -----
for(unsigned long i = 1; i > 0;) { // im not sure if this is nessacery.
if(B1.size() > B2.size())
B2.push_back(0);
else if(B1.size() < B2.size())
B1.push_back(0);
else
i = 0;
}
// -----
""" */

Looks like you want to make the two vectors the same size.
Another way:
{ using std::cout // for NG post
std::vector<bool> vecA(5, true);
std::vector<bool> vecB;
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() > vecB.size()){ vecB.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
while( vecA.size() < vecB.size()){ vecA.push_back(0);}
cout<<"vecA.size()="<<vecA.size()
<<" vecB.size()="<<vecB.size()<<std::endl;
// - reverse the sizes -
std::vector<bool> vecC;
std::vector<bool> vecD(5, true);
while( vecC.size() > vecD.size()){ vecD.push_back(0);}
while( vecC.size() < vecD.size()){ vecC.push_back(0);}
cout<<"vecC.size()="<<vecC.size()
<<" vecD.size()="<<vecD.size()<<std::endl;
/* - output -
vecA.size()=5 vecB.size()=0
vecA.size()=5 vecB.size()=5
vecA.size()=5 vecB.size()=5
vecC.size()=5 vecD.size()=5
*/

}

Only one of the two while() loops (I showed two examples above) will
execute, the other will 'fall through'. If the vectors are the same size,
*both* while() loop conditions will be false, and thus 'fall through'(not
execute).
The *two* while()s, one after the other, look a little strange, but better
than a chain of if()/else, and it pretty much is self commenting code (any
programmer can easily see what it's doing.). Try it, I think you'll like it.
<G>

/* """
unsigned long carry = 0;
for(unsigned long i = 0; i < B1.size(); i++) { // i am sure this can
be done better.
a.push_back(((B1 ^ B2) ^ carry) | ((B1 & B2) &
carry));
carry = (B1 & B2) | (B1 & carry) | (B2 & carry);
}
if(carry == 1)
a.push_back(carry);
return a;}

@code
""" */

If only you could use a 'bitset', it would be TOO easy. <G>

Another thing to consider, you pass the two vectors 'by value'. That means a
copy of each (not too much concern if the size is small). I'd pass them by
'const reference':

std::vector<bool> Add( std::vector<bool>const &B1,
std::vector<bool>const &B2) { /* .... */ }

*BUT*, that won't work because you need to change the vectors inside the
function. You would need to adjust the size before passing them to Add().
Just thought I'd mention that as it's good to think about these things when
designing a class (not as an after thought).

If it needs to be done more than once, I'd make a separate 'private:' helper
function to adjust the sizes:
// in class
private:
bool AdjustSize( std::vector<bool> &B1,
std::vector<bool> &B2 ) /*const*/{
if( B1.empty() && B2.empty() ){
return false; // failure, both empty
} // if()
while( B1.size() > B2.size() ){ B2.push_back(0);}
while( B1.size() < B2.size() ){ B1.push_back(0);}
return true;
} // AdjustSize( vector&, vector&)

std::vector<bool> Add( std::vector<bool> B1,
std::vector<bool> B2){
if( not AdjustSize( B1, B2) ){ throw std::terminate();} // or exit(1);
/* .... */
} // Add(vector, vector)

I'm still a little 'foggy' as to what you are trying to solve overall
exactly. <G>
Is this homework, book excersize, hobby project, <choke>work?

[ corrections, as always, are welcome. ]


Wow, you are helpfull, thanks alot.

yes i can see it would be on its place to explain a thing or two about
what i am doing and why.
It is a hobby project.
First of all im just want to be better at coding by practice, im doing
that by giving my self a challence, in this case, writing a program to
test weather 2^p-1 is prime (mersenne prime). im gonna handle numbers
with millions of digits, so of course i cant be limited by 32/64 bit
vars.
There is a very simple method for testing for testing mersenne primes,
called the lucas lehmer test.

For example, to prove 2^7 - 1 is prime:

@copy
S (1) = 4
S (2) = (4 * 4 - 2) mod 127 = 14
S (3) = (14 * 14 - 2) mod 127 = 67
S (4) = (67 * 67 - 2) mod 127 = 42
S (5) = (42 * 42 - 2) mod 127 = 111
S (6) = (111 * 111 - 2) mod 127 = 0
-------------------------------------
2^P means 2 to the Pth power.
2^7 - 1 = 128 - 1 = 127
Each step except for the initialization step S(1) = 4
takes the value of the previous step, squares it,
subtracts 2, then mods it by the mersenne number.
@copy

However again we run in to the problem with the limitations if using
regular vars.
Imagine if the we were testing 2^207891-1

But enough with that, this is all really a way for me to figure out
methods to use, because my real goal is to write it in assembly,
including a suitable bootloader, and that is a real challence :D

Anyway, if i succeed, im bet i will be a great deal better both at c++
and assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,975
Members
47,533
Latest member
medikillz39

Latest Threads

Top