arisuchan    [ tech / cult / art ]   [ λ / Δ ]   [ psy ]   [ ru ]   [ random ]   [ meta ]   [ all ]    info / stickers     temporarily disabledtemporarily disabled

/λ/ - programming

structure and interpretation of computer programs.
Name
Email
Subject
Comment

formatting options

File
Password (For file deletion.)

Help me fix this shit. https://legacy.arisuchan.jp/q/res/2703.html#2703

Kalyx ######


File: 1494671541628.png (49.95 KB, 517x384, tLh8r.png)

 No.106

I saw Project Euler mentioned somewhere in a thread, and after visiting the site, I decided to try to learn programming by going through it. Sadly, this newfound resolve was short-lived because I couldn't get past the first problem:

>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of >these multiples is 23.

>Find the sum of all the multiples of 3 or 5 below 1000

I used batch to make the program, and it outputs the right answer when I set the multiple limit to under 10, but it won't work for multiples below 1000. I have no idea what I'm doing wrong.
Here's the script:

@echo off
REM Initial Variables
:Start
set /a third=0
set /a fifth=5
set /a numend=1000
set /a ttotal=0
set /a ftotal=0
REM Finding the sum of the Multiples of 3
:ptStart
set /a multiple=1
:tStart
set /a third=%multiple%3
if %third% GEQ %numend% goto :pfStart
set /a ttotal=%ttotal%+%third%
set /a multiple=%multiple%+1
GOTO :tStart
REM Finding the sum of the multiples of 5
:pfStart
set /a multiple=1
GOTO :fStart
:fStart
set /a fifth=%multiple%
5
if %fifth% GEQ %numend% goto :end
set /a ftotal=%ftotal%+%fifth%
set /a multiple=%multiple%+1
GOTO :fStart
:end
set /a fsum=%ttotal%+%ftotal%
echo %fsum%
pause

 No.107

>>106
This has to be a troll, either that or you just enjoy punishment, why on earth would you choose Batch as your language to learn?

 No.108

My guess is that you add numbers that are multiplies of both 3 and 5 twice to the result.

 No.110

>>107
I'm planning on learning ruby, but I didn't have access to it at the time I wrote the script. I (wrongly) assumed that the first problem would be fairly easy to solve, so I went with batch.
>>108
That was the problem, thanks for the help!

 No.112

>>106
If you're trying to learn programming, I would suggest learning Python. If you like the challenge system that Project Euler has, you can try other sites like HackerRank until you start to get the hang of it.

I however would not suggest using Batch for these types of challenges.

 No.113

>>110
>>112
I'm not a fan of javascript, but it is a full fledged programming language that comes with every proper web browser. There are a bunch of online tools for writing JS or you can load it offline from a file or open a console and use a REPL.

 No.114

>>110
Everything is hard when you use batch.

 No.123

Greetings, fellow neophyte! 😃
I have a tiny little bit of experience in C, but a friend of mine recently had me read The Little Schemer, which has captured my soul. So, I'll be using Racket, instead. Racket is one hell of a language. It's got the flexibility and grace that the Lisps are famous for, to be sure, but it's also got a jungle of almost-identical constructs—I'm looking at you, for family! 😵 That said, it's been a really fun language to play around with, and once I'm a little more confident in my abilities I'm going to tear into the other libraries like a rack of baby-back ribs!

To demonstrate just how flexible Racket is, here's a handful of different ways to solve Problem 1.
#lang racket

(require math/number-theory)

;; Problem 1: Multiples of 3 and 5
;; Find the sum of all the multiples of 3 or 5 below 1000.
; Solution 0: Checks whether num is divisible by 3 or 5
(define (good-num? num)
  (or (divides? 3 num)
      (divides? 5 num)))

; Solution 1: Simple recursion. Needs to be called (sum-of-divisors1 (sub1 max)), else it'll include the max number in addition to the ones below it. 😥
(define (sum-of-divisors1 num)
  (cond
    [(zero? num) 0]
    [(good-num? num) (+ num (sum-of-divisors1 (sub1 num)))]
    [else (sum-of-divisors1 (sub1 num))]))

; Solution 2: Tail recursion. They say it runs way faster this way! 🎾💨
(define (sum-of-divisors2 max)
  (define (sod sum num)
    (cond
      [(zero? num) sum]
      [(good-num? num) (sod (+ sum num) (sub1 num))]
      [else (sod sum (sub1 num))]))
  (sod 0 (sub1 max)))

; Solution 3: Higher-order functions. This is probably the most ✨elegant✨ solution of the bunch!
(define (sum-of-divisors3 max)
  (foldl + 0 (filter good-num? (range max))))

; Solution 4: For loop. There's a bunch of different versions of the for loop in Racket, and a bunch of them could've worked here, but I chose the simplest.
(define (sum-of-divisors-for max)
  (for/sum ([number (range max)])
    (if (good-num? number)
        number
        0)))

(Please don't copy my solutions! If you do, then you won't learn anything! Plus, I'll get really mad, and I'll keep being mad at you until you make up a new one! 😤)

 No.124

In the spirit of using Windows Command tools, a nice Powershell version of this would be

function Get-FizzBuzzSum {
Param(
[Parameter(Position=0)][int]$max
)
(1..($max-1) | Where-Object {($_ % 3 -eq 0) -or ($_ % 5 -eq 0)} | Measure-Object -Sum).Sum
}

Get-FizzBuzzSum 1000

 No.139

>>112
hello im new to these sort of projects like Project Euler, i also heard about OverTheWire: WarGames which looks pretty nifty, and i hadn't heard of Hacker Rank.
What's the consensus on these websites
> how good are they at teaching you things while being fun?
> Are there more websites?
> Which ones are 'better' or how do they compare? (i.e. hacker rank and euler project)

 No.141

>>123

How many layers of irony are you on my man? Are you serious with these emojis or are you making fun of OP / the rest of us? I hope this is just an elaborate attempt at ironic sincerity.

 No.143

>>141

to defend >>123 i have a friend who types just like 123 , unironically. It's sort of a 'personal-aesthetic' kind of thing. It's sincere so i think it's really nice. Also 123's reply is super informative and i love it.

 No.145

>>143

wake me up inside

 No.147

>>141
Just a few gigalayers of irony, young padawan, nothing your puny mortal mind couldn't comprehend with a few lifetimes of study and meditation. 😜 I just like using emojis! Even if they're considered kind of a faux pas on imageboards, due to the social media associations, the Noto emojis especially are cute and fun and brighten up my day, and I know some other people who like them too! 😁

And yes, I even put them in my comments. Modern languages' sources are UTF-8 for a reason, you know! 👍

>>143
Oh, thank goodness. 😌 I was scared making such a big post would annoy people, but I'm glad someone appreciated it!

 No.149

>>139
They are nice, especially for beginners. They are very limited in scope though, so don't limit yourself to them.

 No.154

Why are you brute-forcing this?
You can do this in constant time easily.


The sum is (sum of multiples of 3) + (sum of multiples of 5) - (sum of multiples of 15),
subtracting to avoid double counting.

sum of multiples of n that are less than m is
sum(k=1,floor(m/n),k)*n

use this formula:
> sum(k=1,n,k) = n(n+1)/2

Combining everything, you get a simple and very fast solution.

 No.173

>>154
Damn, we need \TeX tags else this stuff gets really annoying to read.

 No.302

>>106
let rec f sum x = 
    if x mod 3 = 0 || x mod 5 = 0 then 
        f (sum + x) (x - 1)
    else if x > 0 then
    f sum (x - 1)
    else sum
;;

f 0 999;;


Lazily speed coded a pure function in Ocaml.

 No.570


>>106
Here is some scheme code, im currently reading SICP. Feel free to point out improvements or mistakes!

(define (the-soykaf? n)
  (or (= (modulo n 3) 0)
      (= (modulo n 5) 0)))

;; Lang: SICP (PLaneT 1.18)
;; Input: (fizz-sum <POSITIVE INTEGER>)  
;; Output: The sum of all numbers divisable with 3 or 5 up to the provided number.

(define (fizz-sum to)
  (define (fizz-sum-iter count sum)   
     (if (not (> count to))
         (if (the-soykaf? count)
             (fizz-sum-iter (+ count 1) (+ sum count))
             (fizz-sum-iter (+ count 1) sum))
         sum))
   (fizz-sum-iter 1 0))

 No.636

File: 1505130890336.jpg (56.79 KB, 680x472, wall_decal_pi_s.jpg)

I wrote some solutions in several languages in past with some friends. ProjectEuler is better to learn about programming and mathematics. I'll say that you learn more about mathematics than programming, but mathematics is very important. At least is very well accepted, known and developed language for describing logical problems. And you can benefit on programming with mathematical thinking (on daily programming, much more than the mathematics itself and its infinite fields).

Ah, before I forget, the link is this: https://github.com/DestructHub/ProjectEuler

Enjoy programming and make sure to use a great language for start. I didn't will recommend BATCH. Python, Scheme or even C can be a very good start.

 No.657

>>123
You can also use #:when to only traverse certain elements from a for loop

(for/sum ([i (in-range 1000)]
          #:when (zero?
                  (* (remainder i 3)
                     (remainder i 5))))
  i)


I keep considering moving away from Racket to other Lisps (since "nobody uses Racket"), but they never seem to compare. Racket feels awkward and pretty bad at first, but when you learn how to write it effectively, it's excellent. Definitely my favorite programming language. I'm currently building a Lisp->MIPS optimizing compiler, and it's only about 1k lines of code.

 No.668

>>302
let rec f acc =
  let p x = x mod 3 = 0 || x mod 5 = 0 in function
    | x when p x -> f (acc + x) (x - 1)
    | x when x > 0 -> f acc (x - 1)
    | _ -> acc
;;

 No.721

>>147

Denko, is that you? (´・ω・`)

 No.1619

>>570

This is a really elegant solution in Scheme.



[Return] [Go to top] [ Catalog ] [Post a Reply]
Delete Post [ ]