项目欧拉q3

时间:2018-03-29 09:57:09

标签: vba preprocessor

我正在尝试Project Euler q3。代码一直有效,直到我评估600851475143,此时#34;#"如下所示。我已经读过,这可能意味着该值被解释为double,或者表示需要一个"预处理器命令"。

我是编码新手,希望能更好地了解正在发生的事情。

注意:运行代码时,显示的错误是"编译错误:溢出"

突出显示的行是#If 600851475143#Mod intP1 = 0然后'计算因子

感谢您的帮助!

Option Explicit

Public Function Prime() As Integer

Dim intP1 As Double
Dim intP2 As Double
Dim i As Integer
Dim intlar As Double

i = 1

For intP1 = 2 To 600851475143# 'counter to produce the numbers

    If intP1 = 2 Then

        'Debug.Print intP1

    Else

        For intP2 = 2 To intP1 - 1 'check if they are prime

            If intP1 Mod intP2 = 0 Then

                Exit For

            Else

                If intP2 = intP1 - 1 Then

                    i = i + 1

                    'Debug.Print intP1

                        #If 600851475143# Mod intP1 = 0 Then 'calculates factors

                            intlar = intP1

                            Debug.Print intlar

                        #End If

                End If

            End If

        Next

    End If

Next

End Function

修改后的代码

 Public Sub question3()

Dim intP1 As Double
Dim intP2 As Double
Dim i As Double
Dim intlar As Double
Dim A1 As Double

A1 = 600851475143#

i = 1

For intP1 = 2 To A1 'counter to produce the numbers

If intP1 = 2 Then

    'Debug.Print intP1

Else

    For intP2 = 2 To intP1 - 1 'check if they are prime

        If intP1 Mod intP2 = 0 Then

            Exit For

        Else

            If intP2 = intP1 - 1 Then

                i = i + 1

                'Debug.Print intP1

                    If A1 - (Int(A1 / intP1) * intP1) = 0 Then 'calculates factors

                        intlar = intP1

                        Debug.Print intlar

                    End If

            End If

        End If

    Next

End If

下一步

End Sub

0 个答案:

没有答案