2013-11-04 24 views
0

我有試用Divison算法楓樹下面的代碼:試用Divison算法楓

TrialDivision := proc(n :: integer) 
    if n <= 1 then 
      false 
    elif n = 2 then 
      true 
    elif type(n, 'even') then 
      false 
    else 
      for local i from 3 by 2 while i * i <= n do 
       if irem(n,i) = 0 then 
         return false 
       end if 
      end do; 
      true 
    end if 
end proc: 

這是我從http://rosettacode.org/wiki/Primality_by_trial_division#MATLAB了。但是當我嘗試運行它時,它給了我以下錯誤:錯誤,過程體中的意外local聲明。

有誰知道我在做什麼錯?

+0

本幫助頁面包含楓樹PROC的,甚至內部PROC的http://www.maplesoft.com/support/help/Maple/view.aspx?path=examples/lexical有用的信息,請參閱在最後一個例子頁面,這非常有趣。 – Nasser

回答

1

在整個過程體中允許局部聲明是Maple語言的一種新近增加。

你可以改變它,說。

TrialDivision := proc(n :: integer) 
    local i; 
    if n <= 1 then 
      false 
    elif n = 2 then 
      true 
    elif type(n, 'even') then 
     false 
    else 
      for i from 3 by 2 while i * i <= n do 
       if irem(n,i) = 0 then 
         return false 
       end if 
      end do; 
      true 
    end if 
end proc: