Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Finding perfect numbers BillMcEnaney Programmer named Tim Posts: 8 Threads: 2 Joined: Apr 2019 Reputation: 0 Likes received: 0 #1 Apr-02-2019, 11:22 PM 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))``` ichabod801 Bunny Rabbit Posts: 3,517 Threads: 73 Joined: Sep 2016 Reputation: 194 Likes received: 1070 #2 Apr-03-2019, 01:30 AM 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 BillMcEnaney Programmer named Tim Posts: 8 Threads: 2 Joined: Apr 2019 Reputation: 0 Likes received: 0 #3 Apr-03-2019, 02:04 AM (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. ichabod801 Bunny Rabbit Posts: 3,517 Threads: 73 Joined: Sep 2016 Reputation: 194 Likes received: 1070 #4 Apr-03-2019, 03:07 AM (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 BillMcEnaney Programmer named Tim Posts: 8 Threads: 2 Joined: Apr 2019 Reputation: 0 Likes received: 0 #5 Apr-03-2019, 05:15 AM (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. micseydel Involuntary Spiderweb Collector Posts: 1,971 Threads: 49 Joined: Sep 2016 Reputation: 43 Likes received: 584 #6 Apr-04-2019, 03:44 AM 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.) BillMcEnaney Programmer named Tim Posts: 8 Threads: 2 Joined: Apr 2019 Reputation: 0 Likes received: 0 #7 Apr-04-2019, 04:46 AM (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```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. 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. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Finding nearest point of a Multidigraph in Python 3.7 stixmagiggins 3 110 Aug-16-2019, 01:32 PM Last Post: ThomasL How to simplify square finding program? meknowsnothing 3 306 Jun-11-2019, 08:20 PM Last Post: meknowsnothing Help with finding correct topic in Python learning yahya01 1 279 Jun-06-2019, 05:01 PM Last Post: buran Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 205 May-09-2019, 12:19 PM Last Post: Pleiades Finding exact phrase in list graham23s 2 220 Mar-13-2019, 06:47 PM Last Post: graham23s finding yesterday and tomorrrow without using date.time module apexman 10 545 Feb-25-2019, 05:33 AM Last Post: samsonite Help with try and open 6.txt file and print as perfect or not Pleiades 13 685 Jan-03-2019, 10:14 PM Last Post: Pleiades Finding phrases of one semantic meaning VladislavMz 2 276 Dec-20-2018, 03:29 AM Last Post: VladislavMz finding problems connecting python to sqlite Dennis 1 289 Dec-10-2018, 02:58 PM Last Post: Larz60+ Finding files knollfinder 2 324 Dec-09-2018, 08:03 AM Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)