[Tutor] Actual code that illustrates problem

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Aug 17 10:02:20 CEST 2006


> #    def strongfac(z,w):
[function body cut]

Ok, let's go through this step by step.

     * What is the intent of strongfac?
     * What are the inputs?  What is 'z', and what is 'w'?
     * What are the outputs?  What is the return value of strongfac?

Same questions for fermat().  What are the inputs and outputs, and what's 
the intent?

Get those out and documented, because although I think I can guess at it, 
I'd rather know that we are sharing the same understanding.


There's a bit of code in strongfac() that's nonsensical from a purely 
technical sense.  For example:

> #                    face = [x,y,0]
[some code cut]
> #                    face[0] = x
> #                    face[1] = y
> #                    face[2] = 0

The three assignments to face[0] through face[2] don't do anything 
effective and clutter the code.



In fermat(), there are magic numbers:

#        for j in range(2,3):
                         ^^^^

#            for k in range(100):
                             ^^^

These seem pretty arbitrary.  Can you explain them?



The strongfac() function you have is a good case for writing smaller 
functions: I can not tell where the function really ends.  I see that you 
have several sub-tests, which I'll categorize as:

     * P-1
     * square = square
     * ...

and there's a few others that I can not categorize yet.  But I bet you 
can.  Have you considered breaking those out as independent helper 
functions?


Most of us don't have experience with number theory.  What people here on 
the list have is experience with writing software.  My experience informs 
me that the functions you're writing are way too large.  They are 
monolithic and imposing enough that very few people will be able to (or 
want to!) understand their reasoning.  They really cry out to be broken 
into smaller subtest functions that can be individually understood.

(And even if your program were running perfectly well, I'd still strongly 
recommend you practice the skill of small, easy-to-understand functions.)



You mentioned later that:

> One of the numbers for which strongfac fails to return factors which it
> correctly calculates is 100000000000037

Two questions:

     * How do you know that it is being "correctly calculated?"

     * How do you know that your program is doing something wrong?

That is, state explicitely to us what you expected the program to do. 
What is the expected result, in terms of return values, that your function 
failed to produce?  What part of the program --- which particular subtest 
--- should have returned those values?  Where are you looking at?


Again, you have to assume that we don't know anything about number theory. 
Things that may seem "obvious" to you are not obvious to this audience. 
*grin*


More information about the Tutor mailing list