posted
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.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
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.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
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.
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
posted
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.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
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.
Posts: 7593 | Registered: Sep 2006
| IP: Logged |
posted
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.
posted
It is apparently an assignment, although conceivably the permute-the-alphabet bit was not the teacher's idea.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
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.
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
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.
posted
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.
IP: Logged |
posted
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.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
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.
quote:Originally posted by BandoCommando: 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.
Posts: 1945 | Registered: Jul 2005
| IP: Logged |
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.
Posts: 1170 | Registered: Jan 2003
| IP: Logged |
posted
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.
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
Blayne Bradley
unregistered
posted
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.
IP: Logged |
posted
When you stopped it, how many had it generated?
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
Blayne Bradley
unregistered
posted
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).
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]; }
//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
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;
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.
Posts: 2596 | Registered: Jan 2006
| IP: Logged |
posted
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.
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
Blayne Bradley
unregistered
posted
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.
IP: Logged |
posted
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.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |