# Thread: I need logic/math help.

1. ## I need logic/math help.

RC = 9 // A number
NB = 2 // A number base
NBR = RC / NB = 9/2 = 4.5 //Amount NB fits in RC

In this case the 0.5 in 4.5 represents the first fragment needed in order for 0.5 * x to equal 1.
If 0.5 * x = 1, then that would represent that the value has reached the number base.
If 0.5 * x = 0.5, then that would indicate that (0.5 * x) represents NB/1, and as x increases it would go on: NB/2, NB/3.. until NB/y = 1.

Do you understand what i am trying to say? Because i need help to solve this for any number base, and i do not carry much math knowledge so i would appreciate if you could help me. The method needs to work for any Number Base. If you don't understand what i am trying to say, please leave a comment so that i can try to explain it further.

2. ## Re: I need logic/math help.

Wait, i solved it.. I just do 1/NB to get the first fragment..

3. ## Re: I need logic/math help.

No, I don't understand what you are asking. What is $\displaystyle x$?

4. ## Re: I need logic/math help.

I needed 1/NB to find this, double NBR - int NBR) / (1/NB) = what number of 1/NB in the fragment.

5. ## Re: I need logic/math help.

To avoid confusing myself, I changed the variables as follows:
a = RC
b = NB

Let $\displaystyle a, b \in \mathbb{Z}_{\geq 0}$
Solve for $\displaystyle x$ when $\displaystyle \left[ \frac{a}{b} - \text{int}\left(\frac{a}{b}\right) \right] x = 1$
$\displaystyle x = \frac{1}{ \left[ \frac{a}{b} - \text{int}\left(\frac{a}{b}\right) \right] }$

Now let's examine the expression $\displaystyle \left[ \frac{a}{b} - \text{int}\left(\frac{a}{b}\right) \right]$
There exists a unique $\displaystyle q \in \mathbb{Z}_{\geq 0}$ and $\displaystyle r \in \[0,b)$ such that $\displaystyle \frac{a}{b} = q + \frac{r}{b}$
It follows that $\displaystyle \text{int}\left( \frac{a}{b} \right) = q$
Therefor $\displaystyle \left[ \frac{a}{b} - \text{int}\left(\frac{a}{b}\right) \right] = \left(q + \frac{r}{b}\right) - (q) = \frac{r}{b}$

So finally $\displaystyle x = \frac{1}{ \left( \frac{r}{b} \right) } = \frac{b}{r}$ where $\displaystyle r$ is the remainder of $\displaystyle a \div b$

For example, if a = 5, b = 3
$\displaystyle 5 \div 3 = 1 + \frac{2}{3}$
You need to multiply $\displaystyle \frac{2}{3}$ (the fractional part) by $\displaystyle \frac{3}{2}$ (b = 3, r = 2) to get $\displaystyle 1$
i.e. $\displaystyle \frac{2}{3} \times \frac{3}{2} = 1$

Note: in the case of base 2, the remainder of non-reducible fractions will always be 1, that might be why you got 1/b.
[What was the point of all this BTW?]

6. ## Re: I need logic/math help.

I didn't quite understand this: q E Z >(with underscore) and R E [0,b)

I'm using it like this:
Say NB = 3, RC = 5, NBR = 5/3 = 1.66666, 1/NB = 1/3 = 0.33333

(NBR - int NBR) / (1/NB) = 2 // Each 1/NB represents 1.

This method appears to work for all number bases, NBR = 422 / 382 = RC / NB = 1.1047...
1/382 = 1/NB = 002617...
(NBR - int NBR) / (1/NB) = 40.007.. = 40;
422 - 382 = RC - NB = 40, i am 40 over, which i just realized i could do too in the case RC > NB.

I am trying to write an algorithm to generate fixed ASCII combinations, that follow the rule of this algorithm, such that RC 1

would always equal the same combination of ASCII letters.
RC = Requested Combination.
R1 = ASCII value start
R2 = ASCII value end
NB = (R2 - R1)+1
NBR = The amount NB repeates in order for it to reach RC.

I have this ARRAY of characters.
4

NBR = 4.5, NB = 2, RC = 9, R1 = 1, R2 = 2;
NBR 1 represents NB*1, ARRAY [1]++, ARRAY[0] = R1;
NBR 2 represents NB*2, ARRAY [1]++, ARRAY[0] = R1;
NBR 3 represents NB*3, ARRAY [2]++, ARRAY[1] = R1;
NBR 4 represents NB*4, ARRAY [1]++, ARRAY[0] = R1;

NBR - int NBR > 0 and NBR - int NBR < 1;
ARRAY [0] = ( NBR - intNBR ) / (1/NB)) = 1

ARRAY[] = 121, the 9'th combination in RC when NB = 2

The algorithm follows the same that our way of counting numbers does.
X = Position in array.

(This method is very slow because it goes through each combination that is required to get RC)
START
If ARRAY[x] < NB then ARRAY[x] = itself + 1
If ARRAY[x] == NB then while ARRAY[x=x+1] == NB, when ARRAY[x] !=NB then ARRAY[x] = itself +1 and while x=x-1 >= 0 thenARRAY[x] =

R1
REPEAT

1
2
11
12
21
22
111
112
121
= 9 different combinations.

I managed to create this algorithm some time ago, but it was too slow.
It would take the rest of my life to go to a combination, so therefore i must do it pure mathematically, then i can go to any

combination of letters in lightning speed, because arithmetic is faster than constantly accessing variables, when you might have to

access the same variables maybe a billion times, depending on what combination you request.

With this new algorithm: RC, NBR, NB, MP, 1/NB, R1, R2
RC = User Defined,
NB = Number Base = (R2 - R1) +1,
NBR = Number Base Repeated = RC / NB,
MP = Max Positions in ARRAY, i have yet to find this mathematically.
1/NB = Can be used to find the left overs from NBR and directly put the value in the ARRAY.

I must do it pure mathematically, which i am close to now.
The only thing remaining is applying the values to ARRAY, and figuring out MP, mathematically-

The thing that confuses my mind is: NBR, and how i apply the values i want based on it.
NBR == 1 would mean ARRAY[1] = R1 AND ARRAY [0] = R1
NBR == 2 would mean ARRAY[1] = R1+1 AND ARRAY [0] = R1
So on until NBR = NB THEN we must create a new position ARRAY[2] = R1 AND ARRAY[1] = R1 AND ARRAY[0] = R1

The first 1/NB in NBR creates position 0
The first 1 in NBR creates position 1
The first NB'th in NBR creates position 2
The first NB^2'th in NBR creates position 3
And it continues like that until it has reached MP and ARRAY[MP] = NB AND all preceeding characters in ARRAY[] = NB
So until the total of ARRAY[] = NB*MP

What i aim for is:
while (MP >= 0)
ARRAY[MP] = Expression
MP-=1

Maybe you could solve the expression required?

7. ## Re: I need logic/math help.

I'll finish reading your response when I get home, but for now:

$\displaystyle a,b \in \mathbb{Z}_{\geq 0}$ mean $\displaystyle a$ and $\displaystyle b$ are non-negative integers (i.e. integers greater then or equal to zero.)
$\displaystyle \mathbb{Z}$ is the set of integers.
$\displaystyle x \in \mathbb{Z}$ means $\displaystyle x$ is contained in the set of integers (i.e. $\displaystyle x$ is an integer, not, for example, any old number.)

$\displaystyle \[0,b)$ means the closed-open (sometimes know as a "half-closed") interval from 0 (inclusive) to b (exclusive).
$\displaystyle x \in \[0,b)$ is the same as saying $\displaystyle 0 \leq x < b$

8. ## Re: I need logic/math help.

I'm not exactly sure what you are trying to do.
Are you looking for a map (a function) that takes natural numbers to unique ASCII sequences?
(Sort of like an inverse hashing function?)

array[0] = "1"
array[1] = "2"
array[2] = "11"
...?

I don't quite understand what you are asking for. By the way, I do program. What language is this in? And could you supply the part of the old code that did what you want. (Maybe then I could follow your logic.)

9. ## Re: I need logic/math help.

The old program is about a half year or so old, and is written in C++. It is super slow, and not very well written.
Do you know how to compile in C++? If so i can send you the _U8CSTRING.cpp file so that you can compile it and test the program to see visually what it does.
If not i can only compile it for windows 64 bit or Gnu/Linux 32 bit or Gnu/Linux 64 bit.
The program doesn't include mathematics at all as you can see, only some addition and subraction. And it will access variables ((R2-R1)+1)^P Times.. And of course printing all the combinations also consumes processing power. With what i was trying to explain in my later posts was doing this except accessing variables ((R2-R1)+1)^P Times, and printing every combination, but instead goto a specific combination with almost pure mathematics and only obtaining that combination.
I don't know if i have made it obvious, but the algorithm can generate any possible combination of letters. Meaning that everything that can be represented with characters exists within that algorithm.
In my earlier post i should have probably called ARRAY[] for CHARACTER[], ARRAY[] is an array of characters.

Code:
#include <iostream>//standard Input/output
#include "_U8CSTRING.cpp"// A string CLASS. _pushnput increases the string at given position, cam is it's lenght.
using namespace std;
const unsigned long long R1 = 48, //First ASCII value
R2 = 57, //Last ASCII value
P = 2;//Max Positions in string
_U8CS _C(P);//String

//Is all characters in _C equal R2?
bool _C_Done_Question(unsigned long long _Beg, unsigned long long _End){
while(_Beg < _End)
switch(_C.str[_Beg++]){
case R2: break;
default: return false;
};
return true;
}

//This is the algorithm.
void generate(){
for(register unsigned long long x = 0;x<P;++x){_C._pushnput(0,R1);}

for (unsigned long long x = (_C.cam-1);_C_Done_Question(0,_C.cam)==false;){
for(;_C.str[x]-1!=R2;++_C.str[x]){cout<<_C.str<<endl;}
--_C.str[x];
if (_C.str[x-1]==R2){
while(_C.str[x]==R2&&x>0)--x;
if(_C.str[x]<R2)
{++_C.str[x];
for(++x;x<(_C.cam);++x){_C.str[x]=R1;}
--x;
}
cout<<_C.str<<endl;
}
else if (_C.str[x-1]<R2){
++_C.str[x-1];
for(;x<(_C.cam);++x){_C.str[x]=R1;}
--x;
cout<<_C.str<<endl;
++_C.str[x];
cout<<_C.str<<endl;
}
}
}
int main(){
generate();
return 0;}

10. ## Re: I need logic/math help.

Yes, I know how to compile C++ code.

11. ## Re: I need logic/math help.

I'm looking through this file (changed the indentation and spacing so I could read it), but I'm not sure what some of these _U8CS functions do: e.g. _U8CS::_pushnput or _U8CS::cam

_U8CS::str I assume holds a c-string representation of your _U8CS (by the way, what's up with these identifier choices? jk, but not rly)
Or it might even just be the data your _U8CS object is working with.

I'll keep looking through the code, and see if I can't better understand what you're trying to do.

[In short, could I see the header file? "_U8CSTRING.cpp"]

Here's my edited version of the code, just in case anyone else was having trouble reading it:
Code:
/*
* Original Author: indieExe on mathhelpforum.com
*
* Edited by: Christopher D'Angelo, mathhead200.com,
*/

#include <iostream> //standard Input/output
#include "_U8CSTRING.cpp" // A string CLASS. _pushnput increases the string at given position, cam is it's lenght.

using namespace std;

const unsigned long long R1 = 48, //First ASCII value
R2 = 57, //Last ASCII value
P = 2; //Max Positions in string
_U8CS _C(P); //String

//Is all characters in _C equal R2?
bool _C_Done_Question(unsigned long long _Beg, unsigned long long _End) {
while( _Beg < _End )
switch( _C.str[_Beg++] ) {
case R2:
break;
default:
return false;
};
return true;
}

//This is the algorithm.
void generate() {

for( register unsigned long long x = 0; x < P; ++x ) {
_C._pushnput(0, R1);
}

for( unsigned long long x = (_C.cam - 1); _C_Done_Question(0, _C.cam) == false; ) {
for( ; _C.str[x] - 1 != R2; ++_C.str[x] ) {
cout << _C.str << endl;
}
--_C.str[x];
if ( _C.str[x - 1] == R2) {
while( _C.str[x] == R2 && x > 0 )
--x;
if( _C.str[x] < R2 ) {
++_C.str[x];
for( ++x; x < (_C.cam); ++x ){
_C.str[x] = R1;
}
--x;
}
cout << _C.str << endl;
} else if ( _C.str[x - 1] < R2 ) {
++_C.str[x - 1];
for( ; x < (_C.cam); ++x ) {
_C.str[x] = R1;
}
--x;
cout << _C.str << endl;
++_C.str[x];
cout << _C.str << endl;
}
}

}

int main() {
generate();
return 0;
}

12. ## Re: I need logic/math help.

I use alot of abbreviations..
_U8CS = unsigned 8bit character string.
tam = total allocated amount,
cam = current 'pseudo' amount,
eam = extra bytes to allocate whenever an allocation is needed,
str = a pointer to unsigned char type which is used as a string,
_pushnput(), literally does what the identifier says to the values in the string.

Code:
//_U8CSTRING.cpp
//This is an incomplete string class.
#pragma once
class _U8CS{
public:
_U8CS(unsigned extraBytes = 20){tam=0,cam=0,eam=extraBytes;alloc();}
~_U8CS(){dealloc();}
unsigned tam,cam,eam;
unsigned char *str;

void alloc(){
str=new unsigned char [tam=eam];
str[cam = 0]='\0';
}

void realloc(){
unsigned char *_new = new unsigned char [tam+=eam];
for(unsigned x = 0;x<=cam;_new[x]=str[x],++x){}
delete[]str;
str = _new;
}
void dealloc(){if(tam!=0&&str!=0){ delete[]str;tam=0,cam=0;str=0;}}

//This method simply moves the the character values fromn this->str[position] to this->str[cam], lenght times for each character.
//In order to make room for the string at arg2.
void _pushnput(unsigned position,unsigned char *str,unsigned lenght){
if( position <= cam && position >= 0 ){
if (lenght+cam >= tam) realloc();
unsigned x = cam+1, y = 0;
while (x>position)
this->str [x+lenght-1] = this->str [--x];
for(x=position;x<position+lenght;)
this->str[x++]=str[y++];
cam+=lenght;
}
}
//The same concept except it only makes room for a single character.
void _pushnput(unsigned position, unsigned char c){
if (!(position <=cam&&position>=0) && !cam) return;
unsigned x = ++cam;
for (;x>position;str[x]=str[--x]){}
str[x] = c;
}

void _remov(unsigned position){
if (! (position<= cam && position >= 0) && !cam ) return;
unsigned x = position;
for (;x<cam;str[x] = str [++x]);
--cam;
}

};

13. ## Re: I need logic/math help.

//Once you have read and understood what the other program does.
If you run this program, you will discover that it is ultra slow.
This is because: NB = (R2-R1) +1, Total Times Accessed A Variable = NB ^ P. ( P = Lenght of string. )
In the case NB = 26, and P = 400. The amount of times accessed a variable is 26^400, which is a ridiculously long number, and it will probably not even fit into a type of unsigned long long.

What needs to be rid of:
1. Fixed lenght for _C (The string containing the combination).
2. NB^P times accessing variables.

What remains is: Only the concept of emulating this algorithm through pure mathematics. (The concept of generating a combination that will always equal that combination, no randomness involved.)

What i came up with when i tried this(And i am bad at mathematics, i only know 1st to 2st year of secondary school equations):
Abbreviations:
RC = Requested Combination. (User Defined).
NB = Number Base.
NBR = Number Base Repeated.
NBRFR = Number Base Repeated Fragment Repeated. (The left over from NBR translated into 1's instead of NBRFF's).
NBRFF = Number Base Repeated First Fragment. (First Fragment Representing NB/1. This value is required to determine NBRFR).
R1 = Range1. (User Defined).
R2 = Range2. (User Defined).
//----------
Definitions:
NB = (R2 - R1)+1. (Such that say 49 - 48 = 2, and not 1).
NBR = RC/NB.
NBRFF = NB^-1 = 1/NB.
NBRFR = (NBR - int NBR) / NBRFF.
//----------

LABEL 1. //I might have to refer to this text later.
The thing about NBR (At least what my logic tells me):
You know that for each first NB^-1, NB^0, NB^1, NB^2, NB^3, NB^4 ET CETERA, in NBR there's a new position in
_C.str (The string containing the ultimate combination).
According to that logic for every NB^-1, NB^0, NB^1, NB^2, NB^3, NB^4 ET CETERA,
that comes after the first (what i said in previous sentence.) will not create a new position in _C.str (because it has
already been created.), but rather increase the value of _C.str[Current_Position+1], and reset the values of
_C.str[Current_Position] to R1 til Current_Position-- == _WHATEVER_THE_END_IS.

IF NBRFR is not equal to ZERO.
This indicates that there is NB/NBRFR left that has not yet been added to _C.str.
(I am currently unsure of where in _C.str to put it. I have a hunch it is in _C.str[0].)

What the mathematical challenge is: LABEL 1. ->
(Determining the amount of positions in _C.str required, Applying the values required to the correct
positions in _C.str).

Sort of like:
while (!LASTPOSITIONIN_C) /*Apply value to _C.str[Current_Position], using a magical formula. ++Current_Position (goto next position in _C.str)*/;

//You may have replied meanwhile i wrote this. Please come with corrections that your perceptive detects, or questions. Or maybe you are able to discover another method that does this?

14. ## Re: I need logic/math help.

This method is broken:
Code:
void _pushnput(unsigned position, unsigned char c) {
if( !(position <= cam && position >= 0) && !cam )
return;
unsigned x = ++cam;
//--- What is this loop doing? ---
for( ; x > position; str[x] = str[--x] ) {
}
str[x] = c;
}
The loop just deincrements x, then copies the character from str[x] back into str[x]...?
As you can see in the following example, the first time this happens, it just deletes the '\0' at str[0]:
Code:
#include <iostream>
#include "_U8CSTRING.cpp"

using namespace std;

int main() {
char x = 'L'; //here to fill the stack, so you can see the '\0' missing
_U8CS s(2);
char y = 'R'; //^ same here ^

cout << s.str << '\n'; //prints "" (empty string)

s._pushnput(0, 'H');
cout << s.str << '\n'; //prints "H??" (for me)

s._pushnput(0, 'w');
cout << s.str << '\n'; //prints "w??" (for me)

return 0;
}
I'm still not entirely sure, what it is you're trying to do... Are you trying to list all the permutations of a set of characters? E.g.
{ 'a', 'b', 'c' }
0 |-> "abc"
1 |-> "acb"
2 |-> "bac"
3 |-> "bca"
4 |-> "cab"
5 |-> "cba"
(This is a guess. I'll keep looking through your code and try to figure out what you are trying to accomplish, as running the program printed some pretty weird results. I'm not giving up on you yet. But I'd like to teach you how to write neater code... )

15. ## Re: I need logic/math help.

The loop just deincrements x, then copies the character from str[x] back into str[x]...?
No, it copies the value from str[x-1] to str[x]. (++identifier is evaluated before an expression in C++)
Try to put the (--x) and (++cam) within parenthesis, what compiler are you using?
(The only fault in this method, is that i doens't check to see if reallocation is required)
Put this:
Code:
if (cam>=tam) realloc();
between the for loop, and
Code:
unsigned x = ++cam
The program above, (the generation program) will iterate every combination that can happen within P positions
when each position has a max value of NB. (It is the same concept that we use for counting numbers.)

Code:
void _pushnput(unsigned position, unsigned char c){
if (!(position <=cam&&position>=0) && !cam) return;
unsigned x = ++cam;//++cam is the position above the '\0' (++cam is evaluated before x)
for (;x>position;str[x/*x is above '\0'*/]=str[--x/*this is the character '\0'*/]){}
//Therefore in the case of cam is 0, str[1] = str[0] = '\0'.
str[x] = c;//All characters have been moved 1 to the right, str[position] is set to c.
}

using namespace std;
int main (){

_U8CS s(0);

s._pushnput(0,'E');
s._pushnput(0,'C');
s._pushnput(0,(unsigned char*)"HELLO",5);
cout<<"\""<<s.str<<"\""<<endl;//Prints "HELLOCE".

return 0;
}
Here is the output of the generation program, R1 = 48, R2 = 49, P = 4.

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111: ((R2-R1)+1)^P = 2^4 = 16. (Notice there is a bug in the program which outputs the same number twice, i removed those occurances in this example.)

As you can see, it follows the same algorithm we use for counting numbers.

Btw, what is a permutation?

Page 1 of 2 12 Last