Need help from you programmers!

A General Discussion forum for cars and other topics, and a great place to introduce yourself if you are new to NICO!
Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

Hey, I need some help. My math teacher said that if I could get show him all the possible combinations of scrambling the word PERPENDICULAR then he'd give me an A for both semesters.

I figure this wouldn't be that difficult to program it into an executable file that wil just keep generating the combonations. I know basic.... well... BASIC.

I know that there are over 700 million combonations. I was thinking of something that would just write them into a text file or something. I wasn't planning on printing this. I was planning on giving it to him on a disc or CD or something. Anybody want to lend me a hand?


User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

yeh, i program. I did this last year as an exercise. Give me some time to write something...

DAEDALUS
Posts: 5421
Joined: Mon Jul 22, 2002 8:50 pm
Car: 1990 Infiniti Q45

Post

Does each solution use all letters, or one or more?

Either way, one tricky thing will be to eliminate all the duplicates, since P, E and R are repeated.

Another tricky thing is that even using the max number of letters in each combination, I think there are over 6 billion possibilities (with duplicates). If one character is 1 byte, the solution wouldn't even fit on a DVD.

SHIFTrl240
Posts: 544
Joined: Mon Jul 07, 2003 4:28 pm
Car: 1990 SR20 Sil-80
2009 Yamaha RoadStar Warrior
2017 Alfa Romeo Giulia Ti AWD
Contact:

Post

this reminds me of something my biology teacher said to the class on the first day :D "if you can write all the numbers from 1 to a million out on paper for me, you will ace the class"

this girl from a different classroom actually got up to something like 120,000 or something, and spent like 5 hrs a day doing it for a few months.... it was crazy.

User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

Daedulus, you would have to use Loops and **** to get the size down.

User avatar
tinted
Posts: 392
Joined: Wed Aug 06, 2003 9:37 am
Car: Computers

Post

when do you need it by?

I could do it maybe in 3 weeks or so depending on how far I get in VB class.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

Loops to make it smaller? Whoah slow down!

There would be a helluva lot of combinations. But if your output is just a text file it would zip up really nice. If i have any free time tonight I'll put something together in C++. I might write a COBOL job. I'll bet our Hoast could take it on in several minutes. I'll get back to you hopefully by monday.

User avatar
tinted
Posts: 392
Joined: Wed Aug 06, 2003 9:37 am
Car: Computers

Post

millions of characters would fill up the memory real fast. Youd have to have a ton of output files So that you could actually open it.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

259,459,200 If you use them all. (Each answer is at least 13 letters long) If i remember how to do permutations right.

N!/(N-R)! Where N is the number of positions and R is the number of unique items

That isn't bad at all. Now i just gotta figure out how to code it.

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

solution available - the program described here constructs a list of all permutations of a word:

http://www.math.grin.edu/~ston...xhtml

it's written in a dialect of Lisp called Scheme :D

p.s. i think the number of permutations of a 13 letter word is 13! = 6,227,020,800 (much larger than 700mil), if each character takes 1 byte, then your resulting (uncompressed) text file will be about 87GB

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

the reason it is less is becasue some of the letters are repeated. Their are only 9 possible letters.

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

RobDET wrote:the reason it is less is becasue some of the letters are repeated. Their are only 9 possible letters.
you're right, i didn't take the repeated letters into account. there are 10 unique letters in PERPENDICULAR, so it does make sense that the total is less than my 13! :pface

i don't think n!/(n-r)! works here. take the word DOD for example, there are 3 permutations: DOD, ODD, DDO. according to you there should be 3!/(3-2)! = 6.

i think in this case the correct formula is:

n!/(n1!n2!...nt!)

it's used for counting generalized permutations, it applies if a sequence of n items has n1 identical items of type 1, n2 identical items of type 2, ..., nt identical items of type t.

so for the word DOD:3!/2! = 3

and for the word PERPENDICULAR:13!/(2!2!2!) = 778,377,600

User avatar
GEO
Posts: 6449
Joined: Mon Jul 07, 2003 3:15 pm
Car: 95 240sx KA-T
Contact:

Post

You would have to use arrays , gotoxy 's and loops..

PERPENDICULAR int z=p,y=e,x=r,w=p,v=e,u=n,t=d..ect

while(x=1, x++, x>13) { gotoxy (12,3); cout<<z<<y<<

}

have fun I started it for you

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

Vengeance wrote:You would have to use arrays , gotoxy 's and loops..

PERPENDICULAR int z=p,y=e,x=r,w=p,v=e,u=n,t=d..ect

while(x=1, x++, x>13) { gotoxy (12,3); cout<<z<<y<<

}

have fun I started it for you
i don't know what gotoxy() is, but it's considered extremely bad practice to use GoTo functions (leads to spaghetti code :))

here's a famous paper on the subject:http://www.acm.org/classics/oct95/

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

I've got it. If i'm not laz tonight i'll get the solution. I won't tell you how either :D

C++ has a built in permutter (not a word?) that takes into account double letters.

next_permutation()

Thats the function. I'm gonna get it running tonight.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

[quote=" nomuken

and for the word PERPENDICULAR:13!/(2!2!2!) = 778,377,600 [/quote]

I like your theory (law? Did you reverse engineer it or look it up?)

I'm not sure how i'm gonna verify your answer. Thats one Helluva Counter.

I may not see you guys on NICO for a while while my machine Explodes from running this program. (actuially the output is going to be the killer becasue the Cool C++ funtion doesn't have to remember it's state. It's a REALLY cool function, Definately worth looking up.)

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

Quote »Did you reverse engineer it or look it up?[/quote] looked it up.

next_permutation() is indeed a cool STL function, good find.

p.s if i'm right, then the resulting uncompressed text file will be a little over 10GB :shocked2

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

found this recursive algorithm in "C/C++ Users Journal"

Just writing a function to generate permutations isn't particularly hard. One easy way to tackle the problem is with a recursive approach. If the string you want to permute is n characters long, you execute a loop that makes one pass per character in the string. Each time through the loop you remove character i from the string, and keep it as a prefix. You then print out all the permutations of the remaining substring concatenated with the prefix. How do you get the list of permutations of the substring? By recursively calling the permutation function. The only additional piece of logic you need to include is the test to see if a substring is only one character long. If it is, you don't need to call the permutation function, because you already have the only permutation of the string.

For example, to print the permutations of "abc", you will first strip off the "a" character, and then get the permutations of "bc". To get those permutations, you will first strip off the "b" character, and get a resulting permutation list of "c". You then strip off the "c" character, and get a resulting permutation of "b". The results when combined with the prefix character of "a" give strings "abc" and "acb".

You then repeat the process for prefix "b" and substring "ac", then for prefix "c" and substring "ab".

the implementation given in C++ is only 23 lines long :pface

crzycav86
Posts: 3836
Joined: Tue Aug 05, 2003 1:28 pm
Car: 93 Nissan 240SX KAT

Post

Too bad the college board switched to java this year...

JAVA rocks! GUIs own you!

Rockenreno
Posts: 2891
Joined: Sun Jan 05, 2003 5:48 pm
Car: 1997 BMW M3
Contact:

Post

nomuken wrote:the implementation given in C++ is only 23 lines long :pface
Yeah, this definately should not take long to do. It's very simple, just use a few loops or recursion.

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

The correct equation is

13!/2!(2!)(2!)

It comes out to 700+ million.

Stuntman240
Posts: 198
Joined: Tue Aug 05, 2003 9:01 am
Car: 91 coupe

Post

ARGH!!!! this thread has fried my brain :confused:

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

It's okay, just go drink some motor oil and rebuild the bottom end. You'll be fine.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

GUI 15 t3h sUx0r

I didn't have time last night. Maybe saturday morning...

You're totally getting an A dood!

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

Thanks Rob, I owe you an organ. If ever you need one, I'll be the first to donate!

nametakennow
Posts: 10024
Joined: Sat Aug 24, 2002 4:14 pm
Car: '06 MINI Cooper S

Post

Wow... my brain is about to implode. Math sucks.

I'm a writer for a reason.

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

Heh, yeah, me too. I love writing.

DAEDALUS
Posts: 5421
Joined: Mon Jul 22, 2002 8:50 pm
Car: 1990 Infiniti Q45

Post

Any pool on run-time? How about size of output file? Get your DVD-Rs ready, and don't forget about the OS-imposed limitations on RAM usage!

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

running the program that uses the recursive algorithm (so slow :pface ) that i showed above. so far taking over 6 hours on an Alpha EV67 with 1GB of RAM.

so far the resulting text file is a little over 16GB, and looks like this:

-----------------------------------PERPENDICULARPERPENDICULRAPERPENDICUALRPERPENDICUARLPERPENDICURLAPERPENDICURALPERPENDICLUARPERPENDICLURAPERPENDICLAURPERPENDICLARU...

ECPDPRIRAUNLEECPDPRIRAULENECPDPRIRAULNEECPDPRIRALENUECPDPRIRALEUNECPDPRIRALNEUECPDPRIRALNUEECPDPRIRALUENECPDPRIRALUNEECPDPRIERNULA-----------------------------------

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

wow... maybe I should just hand him the program and say, "there you go... have fun... it'll take a while."


Return to “General Chat”