This is topic How long will it take to calculate the Alphabet? in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=048265

Posted by Blayne Bradley (Member # 8565) on :
 
How long would it take to calculate every possible permutation of the alphabet? I have the program running now.
 
Posted by King of Men (Member # 6684) on :
 
That depends entirely on how efficient your program is, to be sure. But consider that the actual number of permutations is 29!, or 8.8x10^30. I must say I hope you're not writing that to a logfile.

Hang on, the handicapped English alphabet, being for special people, only has 26 letters, so the number is actually only 4x10^26. I stand by my remarks otherwise.
 
Posted by Dagonee (Member # 5818) on :
 
Assuming each permutation is 26 places long, there are 4.03 X 10^26 possible permutations.

So a good long while. If you have a 2GHz processor and each permutation only takes one cycle to calculate, it would take 6,394,144,171 years.

Edit: that would be a little shorter than half the time the universe has existed.
 
Posted by King of Men (Member # 6684) on :
 
You did mean permutations using all 26 elements, right? If you're also counting the ones with 25 elements, 24 elements, and so on, then of course the number goes up a bit. [Smile]
 
Posted by Dagonee (Member # 5818) on :
 
BTW, if you did this recursively, I see a stack overflow in your future.

If you didn't do this recursively, then either a memory allocation failure or stack overflow is in your future.
 
Posted by King of Men (Member # 6684) on :
 
quote:
that would be a little shorter than half the time the universe has existed.
Or roughly one million times as long, depending on whose numbers you believe. [Big Grin]
 
Posted by Dagonee (Member # 5818) on :
 
Either way, Windows Vista is going to be unsupported before he finishes.
 
Posted by King of Men (Member # 6684) on :
 
Well, I dunno. After all, your calculation was for a 2GHz single processor. He might be using a quad-core 4GHz processor, speeding up the calculation by a factor of eight!

Or you could write a virus to take over large parts of the Internet for you, making a massively-parallel computer - perfect for this kind of very subdivisible problem - that could do it in a mere million years or so.
 
Posted by David Bowles (Member # 1021) on :
 
And perhaps take on an irritating personality that can also jump ships across vast distances!
 
Posted by Mucus (Member # 9735) on :
 
Writing a program on a multi-core processor isn't going to automatically make it concurrent, and it is my educated guess that Blayne probably did not code a concurrent program to do the calculation.
 
Posted by Dagonee (Member # 5818) on :
 
I would just allow the executable to accept a starting two-letter combination as a parameter, then run the executable once for each core with appropriately-spaced starting letters.

Lazy-man multithreading. [Smile]
 
Posted by Mucus (Member # 9735) on :
 
I suspect that you would just not have written such a program in the first place [Smile]
 
Posted by King of Men (Member # 6684) on :
 
It is apparently an assignment, although conceivably the permute-the-alphabet bit was not the teacher's idea.
 
Posted by Qaz (Member # 10298) on :
 
I hope you do not find a way to do this, Blayne. Because if you do, the stars might all go out.

http://en.wikipedia.org/wiki/The_Nine_Billion_Names_of_God
 
Posted by Dagonee (Member # 5818) on :
 
We got assigned a permutation calculation that would have resulted in about a megabyte of data once - on trash-80s with 32k and a single floppy drive. But yeah, I doubt the teacher assigned permuting the whole alphabet.
 
Posted by Bokonon (Member # 480) on :
 
quote:
Originally posted by King of Men:
Or you could write a virus to take over large parts of the Internet for you, making a massively-parallel computer - perfect for this kind of very subdivisible problem - that could do it in a mere million years or so.

No offense to him, but considering Blayne's current programming skills, I think creating said massively-parallel distributed program would take longer than just brute-forcing it.

-Bok
 
Posted by Blayne Bradley (Member # 8565) on :
 
Yup, in fact the assignment explicitely states to use recursion. Which works perfectly but I gave up and stopped running the program as I needed to rerun the program with my comments in it. But ya the teacher only expects us to test 5 letters ABCDE but the assignment include the possibility of permuting the entire alphabet as I have A...Z in an array and I have to input n and the first n letters is whats permuted.
 
Posted by King of Men (Member # 6684) on :
 
Well then, you learned something, to wit "Do complexity analysis before starting program runs." But cheer up, I myself found a stupid, stupid mistake in my code today which has held me up for two weeks by making ten-minute fits run over three days, and get wrong results at that.
 
Posted by Dagonee (Member # 5818) on :
 
O(n!) problems get big very, very quickly.
 
Posted by BandoCommando (Member # 7746) on :
 
[Hail] You all impress the heck out of me.

The best I ever got in programming was a modified BASIC on my TI-86 that I used to simplify calculus. Oh, and there was a brief fling with Hypercard scripts in middle school. I used to drive the librarians crazy with scripts that would force restarts after random periods of time elapsed. But then, I would win their adulation and praises by magically 'fixing' the machines.

You all amaze me.
 
Posted by Blayne Bradley (Member # 8565) on :
 
hehehe.
 
Posted by rollainm (Member # 8318) on :
 
quote:
Originally posted by BandoCommando:
[Hail] You all impress the heck out of me.

The best I ever got in programming was a modified BASIC on my TI-86 that I used to simplify calculus. Oh, and there was a brief fling with Hypercard scripts in middle school. I used to drive the librarians crazy with scripts that would force restarts after random periods of time elapsed. But then, I would win their adulation and praises by magically 'fixing' the machines.

You all amaze me.

Likewise. I wrote a Magic 8 Ball program from scratch on my TI-82 back in 9th grade. [Smile]
 
Posted by quidscribis (Member # 5124) on :
 
42!
 
Posted by Jutsa Notha Name (Member # 4485) on :
 
quote:
Originally posted by Dagonee:
BTW, if you did this recursively, I see a stack overflow in your future.

If you didn't do this recursively, then either a memory allocation failure or stack overflow is in your future.

Is this a teasing joke? The only way this would happen is if he were to perform the operation sloppily or require the system to count the permutations as they are performed. I can see how creating an overflow would happen with some of the quick and dirty programming that I've seen those who never learned any of the theory, but if this is for a class wouldn't the theory be covered?

This would more likely cause the system to lock up if done poorly.
 
Posted by Dagonee (Member # 5818) on :
 
Look at his thread on how he planned on doing this. Besides, both classes I've had a permutation assignment for purposes of teaching recursion didn't go into anything about memory management beyond local variable storage prior to that assignment.
 
Posted by Blayne Bradley (Member # 8565) on :
 
mine were created as the program was run I use 2 pointer char arrays since the size of the array isnt all that definable and they were deleted as the recursive function were popped off the stack.
 
Posted by Dagonee (Member # 5818) on :
 
When you stopped it, how many had it generated?
 
Posted by Blayne Bradley (Member # 8565) on :
 
How many permutations? well it only really tells me how many when it finishes, stopping the debugger kinda stops it before it can output that part by 9 factorial is around 362,880 possible permuations (took 5 friggin minutes to finish).

Heres ze code.

code:
/*************************************************************

Permuation Assignment
Take an input string N of ABC...XYZ
and print out every permutation
of that input string.

Blayne Bradley
0463737



*************************************************************/


#include <iostream>
#include <cstring>

using namespace std;

//Globals, constants, arrays
const int MAX = 27;
char Beta[MAX];
char Alpha[MAX] = { "ABCDEFGHIJKLMNOPQRSTUWVXYZ" };
char App[MAX] = "\0";

int N = 0;

//Functions
void Perm( char Beta[MAX], char append[MAX] );

void main()
{

int in = 0;

cout << "Please select how many letters you wish to"
<< " permute N: ";
cin >> in; // N

// Error checking
while ( in < 0 || in > 26 )
{

cout << "Invalid Input: Please select how many letters you wish to"
<< " permute N: ";
cin >> in;


}


cout << " N Letters are: ";

// copying the selected N letters into the
// array passed to the Perm function.
for ( int counter = 0; counter < in; counter++)
{
Beta[counter] = Alpha[counter];
// to show the original string
cout << Beta[counter];
}

cout << endl;

cout << "********************************" << endl << endl;

//the Permutation function
Perm(Beta, App);
cout << endl;
cout << N << " Permuations: " << in << " Letters Chosen" << endl << endl;
system("pause");

}
/*
recursively do permutations.
*/
void Perm( char StringB[MAX], char To_App[MAX] )
{
// find the length of the string passed
int len = strlen(StringB);


if ( len > 0 )
{

for ( int count = 0; count < len; count++ )
{
// create a pointer array because length at runtime
// isnt definable so it is more efficient to create
// a pointer array dynamically with a more specific
// size.
char* string1 = new char[len+1];

int counter;
int next;

/* within this loop for the length of the loop
assign letters from StringB -> String1

*/
for ( counter = 0, next = 0; counter < len; counter++, next++ )
{
if ( counter == count )
{
string1[counter] = StringB[++next];
continue;
}
else
{
string1[counter] = StringB[next];
}
}

string1[counter] = '\0'; //appending null value to end of array

int length = strlen(To_App);
char* To_App1 = new char [length+2]; //creating the append pointer array
//dynamically

// copy the passed Append string to the
// new Append string at length size.
strncpy(To_App1, To_App, length );
To_App1[length] = StringB[count];
To_App1[length+1] = '\0';

// Perm calls Perm recursively here
Perm( string1, To_App1 );

// delete the used pointer arrays as you pop off the stack.
delete []string1;
delete []To_App1;

}
}
else
{
// Cout each completed
// Permuation when size is zero.
cout << To_App << endl;

N++;
}
}


 
Posted by vonk (Member # 9027) on :
 
quote:
Likewise. I wrote a Magic 8 Ball program from scratch on my TI-82 back in 9th grade.
I wrote a pretty neat little maze RPG on my TI-83 in high school. After about 10 minutes of playing you can tell when I got bored because every option leads to a hilarious death.

Other than that, I like to come in these threads and marvel at how little I know about programing. It's a geek to me- err, Greek, that is. [Smile]
 
Posted by Jutsa Notha Name (Member # 4485) on :
 
Okay, Dagonee, you are correct. That's an overflow waiting to happen. [Smile]
 
Posted by King of Men (Member # 6684) on :
 
Given that the assignment was to do it recursively, how would you avoid an overflow for any reasonable size of the input?

Which is, by the way, why this is rather a silly way to teach recursion.
 
Posted by Dagonee (Member # 5818) on :
 
I don't think someone writing this should worry about it too much - as long as safe coding practices are taught eventually, that is.

I would have calculated the number of permutations before making the max 27, though. [Smile]
 
Posted by Blayne Bradley (Member # 8565) on :
 
max 27 is just the max size of the string, generally i whimsically use 100, 27 seemed better since the alphabet is 26 and i figure 26+1 for the '\0'. absolutely does nothing i think once the first n letters are chosen.
 
Posted by fugu13 (Member # 2859) on :
 
Your code took five minutes for 9 characters? I haven't run your code, but I hope not. A naive python implementation takes under 4 seconds without printing, under 40 seconds with calculating, then printing all the output at once, and around a minute and a half printing everything right off.

Here's the slow version (only difference is how/if the printing is done; the function's exactly the same):

code:
#!/usr/bin/env python

import sys

word = sys.argv[1]

def permute(word):
if len(word) == 0:
yield word
else:
for letter_index in range(len(word)):
for permutation in permute(word[:letter_index] + word[letter_index + 1:]):
yield word[letter_index] + permutation

for permutation in permute(word):
print permutation

10 letters does take appreciably longer, though if it doesn't print it only takes around 40 seconds.

Your computer is probably significantly faster than mine, as well.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2