| New Reply |
iteration, recursion, optimization, ... |
Share Thread | Thread Tools |
| Nov28-12, 06:35 AM | #1 |
|
|
iteration, recursion, optimization, ...1 2 3 ...n 1 1 2 3 ...n 1 2 3 ...n 1 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 2 3 ...n 1 which is only marginally more difficult to program but may be easier to think about because it's more general. Look up the C operator "%" Here's an example output from a Mathcad function, which is written in 5 (completely invisible and very short) lines |
| Nov28-12, 07:42 AM | #2 |
|
|
Code:
for n = 1 to 2 do {
... do something with n ...
}
Code:
Do something with 1 and then do the same thing with 2. |
| Nov28-12, 08:07 AM | #3 |
|
Recognitions:
|
There have already been a couple of single loop solutions posted and later deleted from this thread. Since the OP never replied to this thread, I'm wondering if this is just a "wind-up" (to keep us busy).
|
| Nov28-12, 08:28 AM | #4 |
|
|
iteration, recursion, optimization, ...The "Christmas Tree" effect is due to Mathcad's method of formatting arrays and isn't a programming artifact. |
| Nov28-12, 08:59 AM | #5 |
|
|
By "straightforward" I meant "the way it would be taught in school", and a two-dimensional print-out is ideal for teaching nested loops. This is a subject that you can completely understand with only two loops -- one would be missing the point, and three would be unnecessarily complicated. It's like teaching the concept of "plurals" in language lesons.
Incidentally, I was one of the posters whose post was deleted. I managed the output with no loop at all, even though it depended on the number of lines. The same is possible for the more general case with lines including 123...n123...n123...n1. But what you'd learn from the technique involved is not the "loop" concept. |
| Nov28-12, 02:53 PM | #6 |
|
Recognitions:
|
a a b a a b a b a ... Since the OP has never replied to this thread again, and since it doesn't appear to be homework, I thought it would be OK to include the concept from one of the deleted posts, using C, with spoiler tag: Code:
|
| Nov28-12, 04:10 PM | #7 |
|
|
I don't know what this pattern is called: Code:
1
1 2 1
1 2 3 2 1
1 2 3 5 3 2 1
...
It's all about human readability...speed is the added bonus. |
| Nov28-12, 04:52 PM | #8 |
|
|
P.S.: did you really intend the number 5 in the last line of your pattern? If you did, the sequence (1, 2, 3, 5, ...) may need a bit more explaining than my "increase from 1 to the line number" would be offering. |
| Nov28-12, 05:26 PM | #9 |
|
|
Still, if we're talking classroom, then perhaps we ought to consider introducing recursion? (My introduction to the PDP-11 and programming: "Excuse me, Dr X, but how can I analyse all this data in time for the next lab?", "Um, ah, err ... there's a computer just outside the lab. I think there's a manual for it somewhere ...") |
| Nov28-12, 05:50 PM | #10 |
|
|
|
| Nov28-12, 06:47 PM | #11 |
|
|
Here's a working example in JavaScript (because you can see it online): http://jsfiddle.net/Eg3MC/
I'm completely self-taught, so I couldn't say how they teach it at school. I'm pretty sure they wouldn't be teaching you recursion in this way however. I think you're overcomplicating it. For example, this pattern: Code:
a
a b a
a b a b a
a b a b a b a
...
Code:
var n = 4, str = '';
while(n--){
printLine 'a' + str;
str += ' b a';
}
Code:
1
1 2 1
1 2 3 2 1
1 2 3 5 3 2 1
1 2 3 5 8 5 3 2 1
...
Code:
var n = 10, //number of lines to execute
middle = 2, //The current middle number
last = 1, //last loops middle digit
old = 0, //middle digit 2 loops ago
left = '', //left half of the string
right = ''; //Right half of the string
for(i = 0; i < n; i++){
//Reverse the left half
right = left.split(" ").reverse().join(' ');
//Output the entire line
$('#tree').append(left + last + ' ' + right + '<br>');
//Update values
old = last;
last = middle;
middle = last + old;
left += old.toString() + ' ';
}
I'm using JavaScript for the live example just because it's something you can view online. Also I don't know Matchcad either, but I agree, recursion is tricky. Oh PPS: Michael you're right about the speed differences, the compiler does its own optimization at compile time (I keep thinking in terms of scripting languages like JS). |
| Nov28-12, 07:12 PM | #12 |
|
|
Nested loops are one such concept, and a two-dimensional picture (like our trees) seem a good way to apply it. Single loops that "remember" what happend in previous loops are another such concept. Your Javascript programs use this technique, but nowadays global variables are rather frowned upon, so it would need some rather ugly re-writing to be used in school. Recursion is also taught, but mostly using the two standard examples, computing factorials (boooring) and the Towers of Hanoi. They're a bit similar to our trees, but more challenging, since the recursive call needs to know the parameters of the current call. What I'm thinking about most in this whole discussion is not what's the best way to construct the tree diagram, but what the purpose of the whole exercise is, i.e. what is the student supposed to learn by solving it? |
| Nov28-12, 09:37 PM | #13 |
|
Recognitions:
|
|
| Nov29-12, 10:07 AM | #14 |
|
|
![]() (The OP may very well know about loops but just have been having a "Doh!" moment or looking for non-existent "traps" in a straightforward problem (been there, done that and worn holes in the T-shirt!)) |
| Nov29-12, 10:56 AM | #15 |
|
|
Why introduce another level of abstraction? It's been said that math is poetry, and code is too (it's even WordPress' slogan)...how can you possibly argue recursion over something like this: Code:
var n = 4, str = '';
while(n--){
printLine 'a' + str;
str += ' b a';
}
It's more code, it's less readable, and it's slower. Recursion is more meaningful when you are "recursively" going through data, not when you are building it. Perhaps in MathCad, recursion is less verbose and easier to read (honestly, I don't know what I'm looking at in your diagram). Recursion has it's uses, and sometimes, it's the only way to solve a problem. You don't hammer in nails with a screwdriver...unless of course you don't know where the hammer is. |
| Nov29-12, 04:44 PM | #16 |
|
Mentor
|
Speaking of 'work' and 'thinking', programmers generally understand that computers are very stupid machines that can perform calculations very quickly. If the programmer is unwilling or unable to think, he or she is much more likely to cobble together a program that is too slow, not to mention more full of bugs. These aren't "features" that customers are often willing to pay for. |
| Nov30-12, 03:24 AM | #17 |
|
Recognitions:
|
|
| New Reply |
| Thread Tools | |
Similar Threads for: iteration, recursion, optimization, ...
|
||||
| Thread | Forum | Replies | ||
| Proving recursion relations. BFGS non linear optimization | Calculus & Beyond Homework | 1 | ||
| Recursion vs. Iteration | Programming & Comp Sci | 6 | ||
| Recursion | General Physics | 6 | ||
| Recursion | General Math | 0 | ||