Chowla numbers: Difference between revisions

m
syntax highlighting fixup automation
(Added XPL0 example.)
m (syntax highlighting fixup automation)
Line 78:
{{trans|C}}
 
<langsyntaxhighlight lang=11l>F chowla(n)
V sum = 0
V i = 2
Line 115:
k = kk + 1
kk += k
print(‘There are ’count‘ perfect numbers < ’limit)</langsyntaxhighlight>
 
{{out}}
Line 172:
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight lang=Ada>with Ada.Text_IO;
 
procedure Chowla_Numbers is
Line 241:
Put_Prime;
Put_Perfect;
end Chowla_Numbers;</langsyntaxhighlight>
 
{{out}}
Line 296:
=={{header|ALGOL 68}}==
{{Trans|C}}
<langsyntaxhighlight lang=algol68>BEGIN # find some Chowla numbers ( Chowla n = sum of divisors of n exclusing n and 1 ) #
# returs the Chowla number of n #
PROC chowla = ( INT n )INT:
Line 333:
OD;
print( ( "There are ", whole( count, 0 ), " perfect numbers < ", whole( limit, 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 389:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>chowla: function [n]-> sum remove remove factors n 1 n
countPrimesUpTo: function [limit][
count: 1
Line 410:
if (chowla i) = i - 1 -> print i
i: i + 2
]</langsyntaxhighlight>
 
{{out}}
Line 467:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CHOWLA_NUMBERS.AWK
# converted from Go
Line 543:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 597:
 
=={{header|C}}==
<langsyntaxhighlight lang=c>#include <stdio.h>
 
unsigned chowla(const unsigned n) {
Line 628:
printf("There are %u perfect numbers < %u\n", count, limit);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 682:
=={{header|C sharp|C#}}==
{{trans|Go}}
<langsyntaxhighlight lang=csharp>using System;
 
namespace chowla_cs
Line 740:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 795:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang=cpp>#include <vector>
#include <iostream>
 
Line 851:
cout << "There are " << count << " perfect numbers <= 35,000,000\n";
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 904:
 
=={{header|CLU}}==
<langsyntaxhighlight lang=clu>% Chowla's function
chowla = proc (n: int) returns (int)
sum: int := 0
Line 973:
stream$putl(po, "There are " || int$unparse(perfcount) ||
" perfect numbers < 35,000,000.")
end start_up</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 1,026:
=={{header|Cowgol}}==
{{trans|C}}
<langsyntaxhighlight lang=cowgol>include "cowgol.coh";
 
sub chowla(n: uint32): (sum: uint32) is
Line 1,092:
print(" perfect numbers < ");
print_i32(LIMIT);
print_nl();</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 1,146:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang=d>import std.stdio;
 
int chowla(int n) {
Line 1,208:
}
writefln("There are %d perfect numbers <= 35,000,000", count);
}</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 1,266:
{{trans|C#}}
 
<langsyntaxhighlight lang=dyalect>func chowla(n) {
var sum = 0
var i = 2
Line 1,342:
}
print("There are \(count) perfect numbers <= 35,000,000")</langsyntaxhighlight>
 
{{out}}
Line 1,398:
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight lang=text>func chowla n . sum .
sum = 0
i = 2
Line 1,487:
print "There are " & count & " perfect mumbers up to " & s$
.
call main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,545:
 
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: formatting fry grouping.extras io kernel math
math.primes.factors math.ranges math.statistics sequences
tools.memory.private ;
Line 1,572:
count-primes nl 35e7 show-perfect ;
 
MAIN: chowla-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,629:
=={{header|FreeBASIC}}==
{{trans|Visual Basic}}
<langsyntaxhighlight lang=freebasic>
' Chowla_numbers
 
Line 1,712:
Sleep
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,775:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,854:
}
fmt.Println("There are", count, "perfect numbers <= 35,000,000")
}</langsyntaxhighlight>
 
{{out}}
Line 1,913:
=={{header|Groovy}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=groovy>class Chowla {
static int chowla(int n) {
if (n < 1) throw new RuntimeException("argument must be a positive integer")
Line 1,977:
printf("There are %,d perfect numbers <= %,d\n", count, limit)
}
}</langsyntaxhighlight>
{{out}}
<pre>chowla( 1) = 0
Line 2,033:
Uses arithmoi Library: https://hackage.haskell.org/package/arithmoi-0.11.0.0
compiled with "-O2 -threaded -rtsopts"<br/>
<langsyntaxhighlight lang=haskell>import Control.Concurrent (setNumCapabilities)
import Control.Monad.Par (runPar, get, spawnP)
import Control.Monad (join, (>=>))
Line 2,095:
. fmap (chowlaPrimes $ take (10^7) allChowlas)
perfects = chowlaPerfects allChowlas
allChowlas = chowlas [1..35*10^6]</langsyntaxhighlight>
{{out}}
<pre>Using 4 cores
Line 2,150:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang=j>chowla=: >: -~ >:@#.~/.~&.q: NB. sum of factors - (n + 1)
 
intsbelow=: (2 }. i.)"0
countPrimesbelow=: +/@(0 = chowla)@intsbelow
findPerfectsbelow=: (#~ <: = chowla)@intsbelow</langsyntaxhighlight>
'''Tasks:'''
<langsyntaxhighlight lang=j> (] ,. chowla) >: i. 37 NB. chowla numbers 1-37
1 0
2 0
Line 2,197:
25 168 1229 9592 78498 664579
findPerfectsbelow 35000000
6 28 496 8128 33550336</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang=Java>
public class Chowla {
 
Line 2,287:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,348:
 
The "brute-force" computation of the perfect number beyond 8,128 took many hours.
<langsyntaxhighlight lang=jq>def add(stream): reduce stream as $x (0; . + $x);
 
# input should be an integer
Line 2,420:
;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 2,480:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>using Primes, Formatting
 
function chowla(n)
Line 2,520:
 
testchowla()
</langsyntaxhighlight>{{out}}
<pre>
The first 37 chowla numbers are:
Line 2,576:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang=scala>// Version 1.3.21
 
fun chowla(n: Int): Int {
Line 2,636:
}
println("There are $count perfect numbers <= 35,000,000")
}</langsyntaxhighlight>
 
{{output}}
Line 2,645:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang=lua>function chowla(n)
local sum = 0
local i = 2
Line 2,722:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 2,776:
=={{header|MAD}}==
{{trans|C}}
<langsyntaxhighlight lang=MAD> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
Line 2,826:
DONE PRINT FORMAT PRFCNT, COUNT, LIMIT
END OF PROGRAM</langsyntaxhighlight>
{{out}}
<pre>CHOWLA( 1) = 0
Line 2,882:
{{incorrect|Maple| <br><br> The output for Chowla(1) is incorrect. <br><br> }}
 
<langsyntaxhighlight lang=Maple>ChowlaFunction := n -> NumberTheory:-SumOfDivisors(n) - n - 1;
 
PrintChowla := proc(n::posint) local i;
Line 2,910:
countPrimes(1000000);
countPrimes(10000000);
findPerfect(35000000)</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,960:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[Chowla]
Chowla[0 | 1] := 0
Chowla[n_] := DivisorSigma[1, n] - 1 - n
Line 2,971:
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 1000000, 2}]; i
i = 1; Do[If[Chowla[n] == 0, i++], {n, 3, 10000000, 2}]; i
Reap[Do[If[Chowla[n] == n - 1, Sow[n]], {n, 1, 35 10^6}]][[2, 1]]</langsyntaxhighlight>
{{out}}
<pre>25
Line 2,983:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang=nim>import strformat
import strutils
 
Line 3,025:
k = kk + 1
kk += k
echo &"There are {count} perfect numbers < {insertSep($limit, ',')}"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,085:
{{trans|Go}} but not using a sieve, cause a sieve doesn't need precalculated small primes.<BR>
So runtime is as bad as trial division.
<langsyntaxhighlight lang=pascal>program Chowla_numbers;
 
{$IFDEF FPC}
Line 3,190:
Count10Primes(10 * 1000 * 1000);
CheckPerf;
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,249:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use ntheory 'divisor_sum';
Line 3,284:
my @perfect = perfect(my $limit = 35_000_000);
printf "\nThere are %d perfect numbers up to %s: %s\n",
1+$#perfect, comma($limit), join(' ', map { comma($_) } @perfect);</langsyntaxhighlight>
{{out}}
<pre>chowla( 1) = 0
Line 3,335:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">chowla</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
Line 3,385:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %d perfect numbers &lt;= %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
The use of chowla() in sieve() does not actually achieve anything other than slow it down, so I took it out.
{{out}}
Line 3,414:
{{trans|Prolog}}
{{works with|Picat}}
<langsyntaxhighlight lang=Picat>
table
chowla(1) = 0.
Line 3,454:
end,
printf("There are %d perfect numbers less than %d.\n", Count, Limit).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,509:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(de accu1 (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 3,572:
(prinl "Count of primes up to 1000000 = " (countP 1000000))
(prinl "Count of primes up to 10000000 = " (countP 10000000))
(println (listP 35000000))</langsyntaxhighlight>
 
{{out}}
Line 3,627:
 
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang=powerbasic>#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 3,724:
CON.PRINT "press any key to exit program"
CON.WAITKEY$
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 3,782:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang=Prolog>
chowla(1, 0).
chowla(2, 0).
Line 3,847:
length(Nums, PerfectCount),
format('There are ~D perfect numbers < ~D.\n', [PerfectCount, Limit]).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,904:
Uses [https://www.python.org/dev/peps/pep-0515/ underscores to separate digits] in numbers, and th [https://www.sympy.org/en/index.htm sympy library] to aid calculations.
 
<langsyntaxhighlight lang=python># https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
from sympy import divisors
 
Line 3,935:
for i in range(6, 8):
print(f"primes_to({10**i:_}) == {primes_to(10**i):_}")
perfect_between(1_000_000, 35_000_000)</langsyntaxhighlight>
 
{{out}}
Line 4,001:
* Splitting one function for the jit compiler to digest.
 
<langsyntaxhighlight lang=python>from numba import jit
 
# https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
Line 4,034:
print(f" {i:_}")
c += 1
print(f"Found {c} Perfect numbers between [{n:_}, {m:_})")</langsyntaxhighlight>
 
{{out}}
Line 4,043:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
 
(require racket/fixnum)
Line 4,087:
 
(let ((ns (for/list ((n (sequence-filter perfect?/chowla (in-range 2 35000000)))) n)))
(printf "There are ~a perfect numbers <= 35000000: ~a~%" (length ns) ns)))</langsyntaxhighlight>
 
{{out}}
Line 4,144:
(For a more reasonable test, reduce the orders-of-magnitude range in the "Primes count" line from 2..7 to 2..5)
 
<syntaxhighlight lang=raku perl6line>sub comma { $^i.flip.comb(3).join(',').flip }
 
sub schnitzel (\Radda, \radDA = 0) {
Line 4,171:
say "\nPerfect numbers less than 35,000,000";
 
.&comma.say for mung-daal[^5];</langsyntaxhighlight>
 
{{out}}
Line 4,228:
 
=={{header|REXX}}==
<langsyntaxhighlight lang=rexx>/*REXX program computes/displays chowla numbers (and may count primes & perfect numbers.*/
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 1 /*Not specified? Then use the default.*/
Line 4,256:
return s /*return " " " " " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do k=length(_)-3 to 1 by -3; _= insert(',', _, k); end; return _</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; &nbsp; 37 </tt>}}
<pre>
Line 4,336:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang=ruby>def chowla(n)
sum = 0
i = 2
Line 4,388:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 4,442:
=={{header|Scala}}==
This solution uses a lazily-evaluated iterator to find and sum the divisors of a number, and speeds up the large searches using parallel vectors.
<langsyntaxhighlight lang=scala>object ChowlaNumbers {
def main(args: Array[String]): Unit = {
println("Chowla Numbers...")
Line 4,456:
def chowlaNum(num: Int): Int = Iterator.range(2, math.sqrt(num).toInt + 1).filter(n => num%n == 0).foldLeft(0){case (s, n) => if(n*n == num) s + n else s + n + (num/n)}
}</langsyntaxhighlight>
 
{{out}}
Line 4,517:
Uses Grand Central Dispatch to perform concurrent prime counting and perfect number searching
 
<langsyntaxhighlight lang=swift>import Foundation
 
@inlinable
Line 4,600:
print("\(p) is a perfect number")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,656:
{{works with|Visual Basic|6}}
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang=vb>Option Explicit
 
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Line 4,766:
FreeConsole
 
End Sub</langsyntaxhighlight>
{{out}}
<pre>chowla(1)=0
Line 4,821:
=={{header|Visual Basic .NET}}==
{{trans|Go}}
<langsyntaxhighlight lang=vbnet>Imports System
 
Module Program
Line 4,869:
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 4,923:
===More Cowbell===
{{libheader|System.Numerics}}One can get a little further, but that 8th perfect number takes nearly a minute to verify. The 9th takes longer than I have patience. If you care to see the 9th and 10th perfect numbers, change the 31 to 61 or 89 where indicated by the comment.
<langsyntaxhighlight lang=vbnet>Imports System.Numerics
 
Module Program
Line 4,981:
If System.Diagnostics.Debugger.IsAttached Then Console.ReadKey()
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>chowla(1) = 0
Line 5,038:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>fn chowla(n int) int {
if n < 1 {
panic("argument must be a positive integer")
Line 5,104:
}
println("There are $count perfect numbers <= 35,000,000")
}</langsyntaxhighlight>
 
{{out}}
Line 5,164:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight lang=ecmascript>import "./fmt" for Fmt
import "./math" for Int, Nums
 
Line 5,197:
i = i + 1
}
System.print("There are %(count) perfect numbers <= 35,000,000")</langsyntaxhighlight>
 
{{out}}
Line 5,255:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>func Chowla(N); \Return sum of divisors
int N, Div, Sum, Quot;
[Div:= 2; Sum:= 0;
Line 5,291:
[IntOut(0, N); CrLf(0)];
];
]</langsyntaxhighlight>
 
{{out}}
Line 5,347:
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang=zkl>fcn chowla(n){
if(n<1)
throw(Exception.ValueError("Chowla function argument must be positive"));
Line 5,370:
}
c
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>fcn testChowla{
println("The first 37 Chowla numbers:\n",
[1..37].apply(chowla).concat(" ","[","]"), "\n");
Line 5,396:
}
println("There are %,d perfect numbers <= %,d".fmt(count,limit));
}();</langsyntaxhighlight>
 
{{out}}
10,333

edits