How to diagnose a source of failure in F # interactive

I study F # ropes through Project Euler and have run into a problem several times. I write a function, run it in an interactive F # window, and the program freezes there. I suspect that the function is failing, but I am not getting any significant error message that will help me figure out what is going wrong. Is there a way to debug a program running in F # interactive?
As an illustration, here is an example from task 12. FindFirstTriangle (0,0,100) works fine, but when the divisor is around 150, everything gets stuck.
Note: this is not about what is wrong in this code, but how to figure out where everything goes wrong!

let NumberOfDivisors n =
  [1 .. n] |> List.filter (fun i -> n % i = 0) |> List.length;;

let HasMoreThanDDivisors n d =
  if NumberOfDivisors n >= d then
    true
  else
    false

let rec FindFirstTriangle (index, number, divisors) =
  if HasMoreThanDDivisors number divisors then
    number
  else
    let nextIndex = index + 1
    let nextNumber = number + index
    FindFirstTriangle (nextIndex, nextNumber, divisors);;
+3
2

, , " Windows" , , , . ; . F # interactive (Ctrl-. FSI Visual Studio), .

- , , , . .

let NumberOfDivisors n = 
  printf "%d" n // added
  seq {1 .. n} |> Seq.filter (fun i -> n % i = 0) |> Seq.length;; 

let HasMoreThanDDivisors n d = 
  if NumberOfDivisors n >= d then 
    true 
  else 
    false 

let rec FindFirstTriangle (index, number, divisors) = 
  printfn "" // added
  if HasMoreThanDDivisors number divisors then 
    number 
  else 
    let nextIndex = index + 1 
    let nextNumber = number + index 
    FindFirstTriangle (nextIndex, nextNumber, divisors);;

FindFirstTriangle , , .

+3

- :

FP, F #, , .

Euler:

  • . , . , 1 - 100 101, "" , F #.

  • . O (n ^ 2), n = 10000 . 70 , O (n ^ 2) . F # interactive #time, , .

Brain, NumberOfDivisors: http://en.wikipedia.org/wiki/Euler%27s_totient_function

+2

Source: https://habr.com/ru/post/1734975/


All Articles