Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
36
Task/M-bius-function/Forth/m-bius-function.fth
Normal file
36
Task/M-bius-function/Forth/m-bius-function.fth
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
\ Moebius function
|
||||
: mu ( u -- n )
|
||||
\ multiple of 4 so return 0
|
||||
dup 3 and 0= if drop 0 exit then
|
||||
\ even numbers have 2 as a prime factor
|
||||
dup 1 and 0= if 2/ 1 else 0 then >r
|
||||
\ look for odd prime factors up to the square root
|
||||
3 begin
|
||||
2dup dup * >=
|
||||
while
|
||||
2dup mod 0= if
|
||||
tuck / swap
|
||||
2dup mod 0= if
|
||||
\ repeated prime factor so return 0
|
||||
2drop rdrop 0 exit
|
||||
then
|
||||
\ we have another prime factor
|
||||
r> 1+ >r
|
||||
then
|
||||
2 +
|
||||
repeat
|
||||
drop
|
||||
\ prime factor > square root?
|
||||
r> swap 1 > if 1+ then
|
||||
1 and 0= if 1 else -1 then ;
|
||||
|
||||
: main ( -- )
|
||||
." The first 199 Moebius numbers are:" cr
|
||||
." "
|
||||
200 1 do
|
||||
i mu 3 .r
|
||||
i 1+ 20 mod 0= if cr else then
|
||||
loop ;
|
||||
|
||||
main
|
||||
bye
|
||||
15
Task/M-bius-function/PascalABC.NET/m-bius-function.pas
Normal file
15
Task/M-bius-function/PascalABC.NET/m-bius-function.pas
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
uses school;
|
||||
|
||||
function mobius(n: integer): integer;
|
||||
begin
|
||||
var factors := n.Factorize;
|
||||
if factors.Count = factors.ToSet.Count then
|
||||
result := if factors.Count.IsEven then 1 else -1
|
||||
else result := 0
|
||||
end;
|
||||
|
||||
begin
|
||||
println('Mobius numbers from 1..99:');
|
||||
for var n := 1 to 99 do
|
||||
write(mobius(n):3, if n mod 20 = 0 then #10 else '');
|
||||
end.
|
||||
36
Task/M-bius-function/Swift/m-bius-function.swift
Normal file
36
Task/M-bius-function/Swift/m-bius-function.swift
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import Foundation
|
||||
|
||||
// Moebius function
|
||||
func mu(number: Int) -> Int {
|
||||
var n = number
|
||||
if n % 4 == 0 {
|
||||
return 0
|
||||
}
|
||||
var primeFactors = 0
|
||||
if n % 2 == 0 {
|
||||
primeFactors += 1
|
||||
n /= 2
|
||||
}
|
||||
var p = 3
|
||||
while p * p <= n {
|
||||
if n % p == 0 {
|
||||
n /= p
|
||||
if n % p == 0 {
|
||||
return 0
|
||||
}
|
||||
primeFactors += 1
|
||||
}
|
||||
p += 2
|
||||
}
|
||||
if (n > 1) {
|
||||
primeFactors += 1
|
||||
}
|
||||
return primeFactors % 2 == 0 ? 1 : -1
|
||||
}
|
||||
|
||||
print("The first 199 Moebius numbers are:")
|
||||
print(" ", terminator: "")
|
||||
for i in 1..<200 {
|
||||
print(String(format: "%3d", mu(number: i)),
|
||||
terminator: (i + 1) % 20 == 0 ? "\n" : "")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue