In a CPU, does the speed of a calculation affect the heat generated?
Besides clock speed, keep in mind that in a real "big" CPU (one that has caches, a MMU, TLBs, pipelined execution, SIMD, etc) another important factor of how much heat is generated would be how well the instruction flow utilizes the available CPU resources. If you run a program that is memory-intensive, most of the time the CPU would be just starving for data, doing nothing, so the heat produced would be relatively low. A purely computation code with tight loops would heat more. Even more heat could be generated with tightly optimized code that incurs almost no branch mispredictions, uses the SIMD units intensively, makes optimal use of the caches and so forth.

There are programs specifically designed to operate the CPU in this mode - stressing it as much as possible, Prime95 being a prominent example for the PC.

In fact, if a PC already runs Prime95 on all available CPU cores, and then you concurrently start another CPU-intensive application (e.g., 3D rendering), you'd notice that the CPU cools down. This is because it has to timeshare the very heavy Prime95 code (that "lights up all the transistors") with the relatively lower-demand rendering code (which likely has a lot of cache misses and branch mispredictions - those allow the CPU to stop for a while and cool down).

Another thing you should factor in is that usually each CPU has a table with allowable clock speeds, and core voltages associated with each speed. Lower clocks are also matched with lower voltage, as the manufacturer has determined that the CPU will be stable at that voltage. Power consumption varies roughly linearly with clock speed, but quadratically with voltage.