Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding perfect numbers
#1
Everyone, Lisp, Prolog, Python, and Haskell are my favorite programming languages partly because I love functional programming. But I know that Prolog is a logic programming language instead of a functional one.

Anyhow, I wonder what you'll think of this program and whether I know how to reason like a Python programmer.

A perfect number is a number that equals the sum of its divisors.

import math

def divides(dividend, divisor):
    return dividend % divisor == 0

def divisors_of(number):
    last = math.trunc(math.sqrt(number)) + 1
    return [divisor for divisor in range(2, last) if divides(divisor, number)]

def perfect(number):
    return number == sum(divisors_of(number))
Reply
#2
I would have used not number % divisor, and not bothered with the divides function.

Also, your code does work right. It returns False for 6. Going to the sqrt(n) is fine for checking for primes, but if you need all of the divisors it's going to miss lots.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Apr-03-2019, 01:30 AM)ichabod801 Wrote: I would have used not number % divisor, and not bothered with the divides function.

Also, your code does work right. It returns False for 6. Going to the sqrt(n) is fine for checking for primes, but if you need all of the divisors it's going to miss lots.
Ichabod, you mean that my code doesn't work, right? I'll find and correct the bug. Thanks for revealing it.

You make a good point about the divides function. Maybe I'm to OCD about boolean expressions because I'm a purist. So years ago, when my Sun workstation complained that I put a non-boolean expression in a C program's while-loop condition, I replaced it with a boolean one.

Another purist pet peeve of mine is a break statement that stops a loop before the loop condition comes true or gets falsified. That's why I'd much rather force that condition to be true or force it to be false.
Reply
#4
(Apr-03-2019, 02:04 AM)BillMcEnaney Wrote: Ichabod, you mean that my code doesn't work, right?

Yes, sorry, that's what I meant. You just need to run the loop in the list comprehension higher, or divide by each factor to get the other factors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Apr-03-2019, 03:07 AM)ichabod801 Wrote:
(Apr-03-2019, 02:04 AM)BillMcEnaney Wrote: Ichabod, you mean that my code doesn't work, right?

Yes, sorry, that's what I meant. You just need to run the loop in the list comprehension higher, or divide by each factor to get the other factors.
Thank you. I solved the problem just now before I found the quoted post.
Reply
#6
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.)
Reply
#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


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 342 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  Perfect numbers Vidar567 2 1,872 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,745 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Finding line numbers starting with certain string Sutsro 3 2,481 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,661 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,078 Jan-03-2019, 10:14 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,298 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 5,965 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