Möbius function: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: consistency)
Line 574: Line 574:


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>
<lang futurebasic>local fn IsPrime( n as long ) as Boolean
Boolean i, result

include "NSLog.incl"
if ( n < 2 ) then result = NO : exit fn

local fn IsPrime( n as NSInteger ) as Boolean
for i = 2 to n + 1
'~'1
if ( i * i <= n ) and ( n mod i == 0 )
Boolean i, result
result = NO : exit fn

end if
if ( n < 2 ) then result = NO : exit fn
next

result = YES
for i = 2 to n + 1
if ( i * i <= n ) and ( n mod i == 0 )
result = NO : exit fn
end if
next
result = YES
end fn = result
end fn = result


local fn Mobius( n as long ) as long

long i, p = 0, result = 0
local fn Mobius( n as NSInteger ) as NSInteger
'~'1
NSInteger i, p = 0, result = 0
if ( n == 1 ) then result = 1 : exit fn

if ( n == 1 ) then result = 1 : exit fn
for i = 1 to n + 1
if ( n mod i == 0 ) and ( fn IsPrime( i ) == YES )

for i = 1 to n + 1
if ( n mod ( i * i ) == 0 )
if ( n mod i == 0 ) and ( fn IsPrime( i ) == YES )
result = 0 : exit fn
if ( n mod ( i * i ) == 0 )
else
result = 0 : exit fn
p++
end if
else
end if
p++
next
end if
end if
if( p mod 2 != 0 )
next
if( p mod 2 != 0 )
result = -1
else
result = -1 : exit fn
result = 1
else
end if
result = 1 : exit fn
end if
end fn = result
end fn = result


window 1, @"Möbius function", (0,0,600,300)
window 1

NSInteger i


printf @"First 100 terms of Mobius sequence:"
printf @"First 100 terms of Mobius sequence:"


long i
for i = 1 to 100
for i = 1 to 100
printf @"%2ld\t", fn Mobius(i)
printf @"%2ld\t", fn Mobius(i)
if ( i mod 20 == 0 ) then print
if ( i mod 20 == 0 ) then print
next
next


HandleEvents
HandleEvents</lang>
</lang>
{{output}}
{{output}}
<pre>
<pre>