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 Number Four Posts: 4,089 Threads: 90 Joined: Sep 2016 Reputation: 253 Likes received: 1222 #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 Number Four Posts: 4,089 Threads: 90 Joined: Sep 2016 Reputation: 253 Likes received: 1222 #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: 2,064 Threads: 51 Joined: Sep 2016 Reputation: 48 Likes received: 616 #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.) Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) 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 Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 307 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 895 Jan-03-2019, 10:14 PM Last Post: Pleiades Finding prime numbers creslin_black 7 1,070 Jul-20-2018, 02:28 PM Last Post: grjmmr Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 2,110 May-16-2018, 04:56 PM Last Post: Pleiades

Forum Jump:

Users browsing this thread: 1 Guest(s)