Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Newbie question

by AnomalousMonk (Archbishop)
on Aug 23, 2022 at 17:07 UTC ( [id://11146330]=note: print w/replies, xml ) Need Help??


in reply to Newbie question

One path to learning is to take a problem one step further (and then repeat, etc.).

In your OPed code and (I think) in all the replies, the sign of the number being tested is ignored through the use of abs.

What happens if you do not ignore the sign of the number, so that, e.g., -321 becomes the list (-3, 2, 1) with the sign applying to only the first digit? Is any interesting property of the problem or of numbers in general revealed?

Remember that Perl will seamlessly convert numbers back and forth between numeric and string representations. If -321 is represented as '-321', might regular expressions be used to extract substrings that might then be treated as numbers? (Please see perlre, perlretut, and perlreref/perlrequick.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Newbie question
by oldB51 (Sexton) on Aug 24, 2022 at 07:23 UTC

    Thank you AnomalousMonk. I think the point you are making is that when summing digits: if we consider integers of the form abc say then -abc will always sum to 2a less than abc. 2a is always even so abc and -abc must always both be odd or both even. Hence no need to worry about absolute values for this purpose. Please forgive me if I have not expressed this in a correct formal way - it is over 40 years since I did any mathematics. Your point is excellent.

      ... no need to worry about absolute values for this purpose.

      Yes, that's the point I was getting at. I also wanted to hint that you don't even need to be concerned with the numbers themselves, but only with the least-significant bit of each number (in 2s'-complement binary representation). The LS bit determines the odd/even status of a number, so adding up all these bits mod 2 (i.e., masking with 1) gives the answer.

      Please forgive me if I have not expressed this in a correct formal way ...

      No worries. I doubt if I would recognize a correct formal expression of this proposition if you hit me in the face with it.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146330]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found