F# 失败并显示“错误”4”此表达式应具有 int 类型,但此处具有 int 类型 ->整数”

发布于 2024-11-02 11:47:35 字数 1266 浏览 1 评论 0原文

这是我试图在最后一行工作的代码,它失败了:

let rec gcd a b =
    if b= 0 then
        a
    else
        gcd b (a % b);;

let n = 8051
let mutable d = 0
let mutable c = 1
let mutable xi = 2
let mutable yi = 2
let f x = (pown x 2) + (c % n);;
while c < 100 do
    while d = 1 do
        xi <- (f xi)
        yi <- (f(f(yi)))
        printfn "%d%d" xi yi        
        d <- gcd(abs (xi - yi) n)

---------------------以下代码有效;除了 N 上的整数溢出---------

module Factorization


let rec gcd a b =
    if b= 0 then
        a
    else
        gcd b (a % b);;

let n = 600851475143N
let mutable d, c, xi, yi = 1, 1, 2, 2
let f x = (pown x 2) + (c % n);;

let maxN m =int(ceil(sqrt(float m)))
//if (n > maxN(xi)) && (n >  maxN(yi)) then
while c < 100 do
    d <- 1
    while d = 1 do        
        if (maxN(n) > xi) && (maxN(n) >  yi) then
            xi <- f xi
            yi <- f(f(yi))           
            d <- gcd (abs (xi - yi)) n
            //fail
            if d = n then d<-1
            if d <> 1 then printfn "A prime factor of %d x = %d,  y = %d, d = %d" n xi yi d
        else
            xi <- 2
            yi <- 2
            c  <- c + 1;;

Here is the code that I am trying to get to work last line is where it is failing:

let rec gcd a b =
    if b= 0 then
        a
    else
        gcd b (a % b);;

let n = 8051
let mutable d = 0
let mutable c = 1
let mutable xi = 2
let mutable yi = 2
let f x = (pown x 2) + (c % n);;
while c < 100 do
    while d = 1 do
        xi <- (f xi)
        yi <- (f(f(yi)))
        printfn "%d%d" xi yi        
        d <- gcd(abs (xi - yi) n)

---------------------The Following Code works; Except for integer overflow on N---------

module Factorization


let rec gcd a b =
    if b= 0 then
        a
    else
        gcd b (a % b);;

let n = 600851475143N
let mutable d, c, xi, yi = 1, 1, 2, 2
let f x = (pown x 2) + (c % n);;

let maxN m =int(ceil(sqrt(float m)))
//if (n > maxN(xi)) && (n >  maxN(yi)) then
while c < 100 do
    d <- 1
    while d = 1 do        
        if (maxN(n) > xi) && (maxN(n) >  yi) then
            xi <- f xi
            yi <- f(f(yi))           
            d <- gcd (abs (xi - yi)) n
            //fail
            if d = n then d<-1
            if d <> 1 then printfn "A prime factor of %d x = %d,  y = %d, d = %d" n xi yi d
        else
            xi <- 2
            yi <- 2
            c  <- c + 1;;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2024-11-09 11:47:35

除了 @Rangoric 指出的之外,外括号也必须消失,否则柯里化将不起作用:

d <- gcd (abs(xi-yi)) n

In addition to what @Rangoric pointed out, the outer brackets have to go as well otherwise currying won't work:

d <- gcd (abs(xi-yi)) n
冷夜 2024-11-09 11:47:35

哎呀,这里有一些主动提供的提示(@BrokenGlass 正确回答了问题本身)。

首先,您可以在一行中分配所有这些可变变量:

let mutable d, c, xi, yi = 0, 1, 2, 2

其次,简化括号:

xi <- f xi
yi <- f (f yi)

当然,尝试摆脱可变变量和 while 循环。但我将把它留给您,因为我确信您知道您使用递归实现了 gcd

Yikes, here are a few unsolicited tips (@BrokenGlass answered the question itself correctly).

First, you can assign all those mutables in one line:

let mutable d, c, xi, yi = 0, 1, 2, 2

Second, go easy on the parentheses:

xi <- f xi
yi <- f (f yi)

And of course, try to get rid of the mutables and while loops. But I'll leave that to you since I'm sure you are aware seeing that you implemented gcd using recursion.

小巷里的女流氓 2024-11-09 11:47:35

尝试:

d <- gcd (abs(xi-yi)) n

指出abs 是一个int->int 而不是int 本身。将其括在括号中会导致在 gcd 查看它之前执行ABS。这导致gcd看到abs的结果而不是abs本身。

Try:

d <- gcd (abs(xi-yi)) n

It is pointing out that abs is a int->int and not an int by itself. Wrapping it in parentheses causes the abs to be executed before gcd looks at it. This causes gcd to see the result of abs instead of abs itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文