Skip to content

Instantly share code, notes, and snippets.

@fornext1119
Last active December 19, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fornext1119/5881330 to your computer and use it in GitHub Desktop.
Save fornext1119/5881330 to your computer and use it in GitHub Desktop.

Project Euler を はじめから

4ヶ月もほったらかしになってたので、 復習がてら Problem 1 からやってみる。

F #

初項a、公差d の等差数列の初項から第n項 (末項 l=a+(n-1)d) までの和Snは...

let sn a lim =
  let n = lim / a in
  let l = a   * n in
  (a + l) * n / 2
;;
> let sn a lim =
-   let n = lim / a in
-   let l = a   * n in
-   (a + l) * n / 2
- ;;

val sn : int -> int -> int
let multiples_of_3_and_5 n =
  if   n = 15 then -(sn n 999)
  else               sn n 999
;;
> let multiples_of_3_and_5 n =
-   if   n = 15 then -(sn n 999)
-   else               sn n 999
- ;;

val multiples_of_3_and_5 : int -> int
[3;5;15]
|> List.map (fun n -> multiples_of_3_and_5 n)
;;
> [3;5;15]
- |> List.map (fun n -> multiples_of_3_and_5 n)
- ;;

val it : int list = [166833; 99500; -33165]
[3;5;15]
|> List.map multiples_of_3_and_5
;;
> [3;5;15]
- |> List.map multiples_of_3_and_5
- ;;

val it : int list = [166833; 99500; -33165]
[3;5;15]
|> List.map multiples_of_3_and_5
|> List.sum
;;
> [3;5;15]
- |> List.map multiples_of_3_and_5
- |> List.sum
- ;;

val it : int = 233168
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment