miércoles, 12 de diciembre de 2018

C++ Program: Armtrong nickname generation

C++ Program: Armtrong nickname generation

This is a sample C++ program that computes configurable length random "nicknames" and selects those nicknames that verifies "Armtrong property".

Armtrong property:
Given the number x1x2x3... it verifies Armtrong if : x1x2x3... = x1^3 + x2^3 + x3^3...
An example is the number 153 = 1^3 + 5^3 + 3^3

However!! this program works with a different criteria: which is x1x2x3...= x1*3+x2*3+x3*3....

For the iterations and nickname lengths specified in the code bellow, we get this result:

Note: Works fine with Codeblocks and gcc.

armtrongnames.cpp

/*
 * armtrongnames.cpp
 *
 *  Created on: 12 Dec 2018
 *      Author: david
 */

/*
 * This is an example of Armtrong numbers applied to person names generation:
 * Thus, we will map letters to numbers as a=1, b=2,...z=25
 * Then we will get vocals: a,e,i,o,u
 * Next we will generate names: cvcvcv(c) and vcvcvc(v), in the same time we will check if the number mapping is an Armtrong number
 * Finally we will keep the Armtrong names: What this will give us?
    Armstrong number (modified): x1x2...xn = x1*3+x2*3+...+xn*3
 */


#include
#include
#include
#include
#include
#include
#include

using namespace std;




int isVocal(int pos,vector vocalsW) {
	int r = -1;

	for(int i=0;i vocalsW,string letters){
	int r = -1;
	if(pos>0 && pos<=letters.length()){
		r =isVocal(pos,vocalsW);

		if(r==0)
			r=-1;
		else
			r = 0;
	}

	return r;

}
char getLetter(int pos,string letters){
	char c = '*';
	if(pos>0 && pos<=letters.length())
		c = letters.at(pos-1);
return c;
}

int getRandVocalPos(vector vocalsW){
	int pos = 0;
    pos = rand() % 5;
    return vocalsW[pos];

}

int getRandConsPos(vector vocalsW,string letters){
	int pos =-1;
	bool end = false;
	while(!end){
		pos = (rand()% letters.length()) +1;
		if(isConsonant(pos,vocalsW,letters)==0){
			end = true;
		}
	}

	return pos;
}

int compDec(vector v) {
	int n = 0;

	int i =  v.size() - 1;

	int pw = 0;
	while(i>=0){
		if(v[i] != 0 ){
			n = n + (v[i] * (int)(pow(10,pw)));
			pw++;
		}
	  i--;
	}
	return n;
}
int compArmtrong(int s){
	int a=0;
	int i = 1;
	int b = s;
	int c=0;
	while(b>1){
		c = b % i;
		b = (b-c)/i;
		i = i * 10;
		a = a + (c*3); /*a = a + (int) pow(c,3); */
	}
return a;
}
int sumVect(vector v) {
	int a=0;
		for(int i=0;i v){
	int r = -1;
	int ams = -1;
    int sum = sumVect(v);

	ams = compArmtrong(sum);

	if(ams==sum)
		r = 0;
	return r;
}

string buildName(vector v,string letters){
	string name("");
	char c;

	for(int i=0;i::iterator iter;

std::string letters("abcdefghijklmnopqrstuwxyz");
vector vocalsW;

int MAXITER = 900;
int MAXNAMELEN = 20;
int MINNAMELEN = 4;
int T = MAXITER * ((MAXNAMELEN-MINNAMELEN) + 1);
std:vector *amsnames = new vector();

vocalsW.push_back(1);vocalsW.push_back(5);
vocalsW.push_back(9);vocalsW.push_back(15);
vocalsW.push_back(21);


int i = 0;
while(i0){
    vector nameW;

        if(altern%2==0){ //first a consonant

            for(int k=1;k<=j;k++){
                if(k%2!=0)
                    nameW.push_back( getRandConsPos(vocalsW,letters) );
                else
                    nameW.push_back(getRandVocalPos(vocalsW));

            }

        }
        else {  //first a vocal
            for(int k=1;k<=j;k++){
                if(k%2!=0)
                    nameW.push_back( getRandVocalPos(vocalsW) );
                else
                    nameW.push_back(getRandConsPos(vocalsW,letters));

            }
        }

		int r = isArmtrong(nameW);
        string name = buildName(nameW,letters);
        cout << name << endl;
        if(r==0)
			amsnames->push_back(name);

        altern--;

        }
		j++;
	}
	i++;
}

cout << "+++++++++++++++++++++++++" << endl;
cout<< "Generated Armtrong Names:" << amsnames->size() << endl;
int cnt = 1;
for(iter=amsnames->begin();iter!=amsnames->end();iter++){
	cout<< cnt << " :" << *iter << endl;
    cnt++;
}
getch();
return 0;
}