Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding perfect numbers
#7
(Apr-04-2019, 03:44 AM)micseydel Wrote: If you like functional programming, you might want to checkout Scala. It doesn't have break or continue for its loops, some might find that frustrating but it sounds right up your alley :)

(I actually today wanted to use one of those constructs in Scala, noticed that it could be re-written in a more functional style, and proceeded to do that instead. Also Scala does have some utilities for simulating a break or continue I think, they're basically just exception handling.)
That sounds like a great tip, Mic. I've heard of Scala and glanced at a Scala program or two, so I'll learn more about it because you like it. For me, THE language to admire us Haskell because you can write gorgeous code in it. I could hardly believe my eyes when I read a recursive quicksort that some clever programmer crammed into three lines. But I beauty won't convince any thoroughly practical programmer to trade, say, C++, for concise elegance. I prefer theory to practice. Since I've stopped programming professionally, I don't need to care what languages are popular in the industry.

qsort :: (Ord a) => [a] -> [a]
 qsort []     = []
 qsort (x:xs) = qsort less ++ [x] ++ qsort more
     where less = filter (<x)  xs
           more = filter (>=x) xs
To shorter that program, you move the filter function calls to the lined with the ++, concatenation, operators on it and leave out the line with the "Ord" on it. Since the program is polymorphic, your computer can use it to sort any sortable data.

I feel almost embarrassed to admit that before I tutored computer science students, I programmed professionally in Cobol. Cry Since then, the standard probably has changed to remove the "alter" statement, a truly evil invention. When Cobol first came out, main memory was tiny. To save it, the altar statement would make the machine do something awful.

After you typed "ALTER 250-PRINT-PAYROLL-REPORT" TO PROCEED TO 350-CALCULATE-STATE-INCOME-TAX.", each time the program seemed to tell the machine to print the payroll report, it would calculate the income tax instead. So if you wanted to torture your enemies, you could include a file full of alter statements to make the program behave unintelligibly. The trick is to hide the alter statements from the reader.

I haven't pulled that sadistic stunt.
Reply


Messages In This Thread
Finding perfect numbers - by BillMcEnaney - Apr-02-2019, 11:22 PM
RE: Finding perfect numbers - by ichabod801 - Apr-03-2019, 01:30 AM
RE: Finding perfect numbers - by BillMcEnaney - Apr-03-2019, 02:04 AM
RE: Finding perfect numbers - by ichabod801 - Apr-03-2019, 03:07 AM
RE: Finding perfect numbers - by BillMcEnaney - Apr-03-2019, 05:15 AM
RE: Finding perfect numbers - by micseydel - Apr-04-2019, 03:44 AM
RE: Finding perfect numbers - by BillMcEnaney - Apr-04-2019, 04:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 403 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  Perfect numbers Vidar567 2 1,934 Nov-23-2020, 10:29 PM
Last Post: Vidar567
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,853 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Finding line numbers starting with certain string Sutsro 3 2,579 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,770 May-09-2019, 12:19 PM
Last Post: Pleiades
  Help with try and open 6.txt file and print as perfect or not Pleiades 13 5,285 Jan-03-2019, 10:14 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,417 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 6,106 May-16-2018, 04:56 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020