CS100-Very Hard Problems.pptx

advertisement
Very Hard Problems
I am so not kidding about this.
Please grab a handout.
Theory
What do you think?
There are some well-formed mathematical
problems that computers can’t solve, no
matter how fast computers get.
1. True
2. False
Java Programs That Take Files as Inputs
For example:
CountOfWordsInFile.java
…we run it like this “CountWordsInFile file.txt”
And it prints the number of words in that file.txt.
What if we ran it like this:
“CountWordsInFile CountWordsInFile.java”
What would it print?
1. That doesn’t make sense
2. It would run forever
3. The number of words in the CountWordsInFile.java
file (say 100)
Ok, another program
•
•
•
•
•
RunForever.java
It’s a program with an infinite loop
…we run it like this “RunForever file.txt”
But no matter what file we put in, it runs forever
So what would happen if we run it like this:
“RunForever RunForever.java”
1. That doesn’t make sense
2. It would run forever
3. It would print something
Still Another Program
• RunWithItself.java
• Just runs the java program passed in, with itself
as input. So “RunWithItself JavaProg.java” is the
same thing as “JavaProg JavaProg.java”
• So say we ran “RunWithItself
CountWordsInFile.java”
1. That doesn’t make sense
2. It would run forever
3. It would print out a number
Penultimate Program
WillRunForever.java
• Really handy program that determines if a
program will run forever *if passed itself as an
input* (it acts a lot like RunWithItself.java)
• Prints either “SAFE” or “DANGER”
• This program never runs forever
• What is the output of:
Thing To Run
WillRunForever CountWordsInFile.java
WillRunForever RunForever.java
WillRunForever WillRunForever.java
And it outputs
Last Program
• StrangeProgram.java
• Runs WillRunForever.java on it’s input. If
WillRunForever.java prints “SAFE”,
StrangeProgram runs forever. If
WillRunForever.java prints “DANGER”
StrangeProgram prints “STRANGE” and exits.
Thing To Run
StrangeProgram CountWordsInFile.java
StrangeProgram RunForever.java
StrangeProgram WillRunForever.java
And it outputs
Final Question
• What is the output of:
StrangeProgram StrangeProgram.java
• Hint: It helps if you figure out the output of
WillItRunForever StrangeProgram.java first
The halting problem is not
computable.
There are others.
• You can see my PNP “slides” here:
• http://prezi.com/k8e5v1wgicit/copy-of-copyof-ghp-pnp/
Download