Chowla numbers: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 74:
:*   the OEIS entry for   [http://oeis.org/A048050 A48050 Chowla's function].
<br><br>
 
=={{header|11l}}==
{{trans|C}}
 
<syntaxhighlight lang="11l">F chowla(n)
V sum = 0
V i = 2
Line 169 ⟶ 168:
There are 5 perfect numbers < 350000000
</pre>
 
=={{header|Ada}}==
{{trans|C}}
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
 
procedure Chowla_Numbers is
Line 293 ⟶ 291:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
<syntaxhighlight 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 386 ⟶ 383:
There are 5 perfect numbers < 350000000
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">chowla: function [n]-> sum remove remove factors n 1 n
countPrimesUpTo: function [limit][
count: 1
Line 465 ⟶ 461:
8128
33550336</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f CHOWLA_NUMBERS.AWK
# converted from Go
Line 595 ⟶ 590:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
 
unsigned chowla(const unsigned n) {
Line 679 ⟶ 673:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
 
=={{header|C sharp|C#}}==
{{trans|Go}}
<syntaxhighlight lang="csharp">using System;
 
namespace chowla_cs
Line 792 ⟶ 785:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|C++}}==
{{trans|Go}}
<syntaxhighlight lang="cpp">#include <vector>
#include <iostream>
 
Line 902 ⟶ 894:
33,550,336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Chowla's function
chowla = proc (n: int) returns (int)
sum: int := 0
Line 1,026 ⟶ 1,017:
=={{header|Cowgol}}==
{{trans|C}}
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub chowla(n: uint32): (sum: uint32) is
Line 1,143 ⟶ 1,134:
33550336 is a perfect number.
There are 5 perfect numbers < 35000000</pre>
 
=={{header|D}}==
{{trans|C#}}
<syntaxhighlight lang="d">import std.stdio;
 
int chowla(int n) {
Line 1,259 ⟶ 1,249:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|Delphi}}==
See [[#Pascal]].
Line 1,266 ⟶ 1,255:
{{trans|C#}}
 
<syntaxhighlight lang="dyalect">func chowla(n) {
var sum = 0
var i = 2
Line 1,395 ⟶ 1,384:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight lang="text">func chowla n . sum .
sum = 0
i = 2
Line 1,543 ⟶ 1,531:
There are 5 perfect mumbers up to 35,000,000
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting fry grouping.extras io kernel math
math.primes.factors math.ranges math.statistics sequences
tools.memory.private ;
Line 1,626 ⟶ 1,613:
33,550,336 is perfect
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Visual Basic}}
<syntaxhighlight lang="freebasic">
' Chowla_numbers
 
Line 1,773 ⟶ 1,759:
Pulsa una tecla para salir
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 1,910 ⟶ 1,895:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|Groovy}}==
{{trans|Kotlin}}
<syntaxhighlight lang="groovy">class Chowla {
static int chowla(int n) {
if (n < 1) throw new RuntimeException("argument must be a positive integer")
Line 2,033 ⟶ 2,017:
Uses arithmoi Library: https://hackage.haskell.org/package/arithmoi-0.11.0.0
compiled with "-O2 -threaded -rtsopts"<br/>
<syntaxhighlight lang="haskell">import Control.Concurrent (setNumCapabilities)
import Control.Monad.Par (runPar, get, spawnP)
import Control.Monad (join, (>=>))
Line 2,147 ⟶ 2,131:
33,550,336 is a perfect number.
There are 5 perfect numbers < 35,000,000</pre>
 
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j">chowla=: >: -~ >:@#.~/.~&.q: NB. sum of factors - (n + 1)
 
intsbelow=: (2 }. i.)"0
Line 2,156 ⟶ 2,139:
findPerfectsbelow=: (#~ <: = chowla)@intsbelow</syntaxhighlight>
'''Tasks:'''
<syntaxhighlight lang="j"> (] ,. chowla) >: i. 37 NB. chowla numbers 1-37
1 0
2 0
Line 2,198 ⟶ 2,181:
findPerfectsbelow 35000000
6 28 496 8128 33550336</syntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
<syntaxhighlight lang=Java"java">
public class Chowla {
 
Line 2,342 ⟶ 2,324:
There are 5 perfect numbers < 35,000,000
</pre>
 
=={{header|jq}}==
{{works with|jq}}
Line 2,348 ⟶ 2,329:
 
The "brute-force" computation of the perfect number beyond 8,128 took many hours.
<syntaxhighlight lang="jq">def add(stream): reduce stream as $x (0; . + $x);
 
# input should be an integer
Line 2,477 ⟶ 2,458:
33,550,336
</pre>
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes, Formatting
 
function chowla(n)
Line 2,573 ⟶ 2,552:
The count of perfect numbers up to 35,000,000 is 5.
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">// Version 1.3.21
 
fun chowla(n: Int): Int {
Line 2,642 ⟶ 2,620:
Same as Go example.
</pre>
 
=={{header|Lua}}==
{{trans|D}}
<syntaxhighlight lang="lua">function chowla(n)
local sum = 0
local i = 2
Line 2,773 ⟶ 2,750:
33550336 is a number that is perfect
There are 5 perfect numbers <= 35,000,000</pre>
 
=={{header|MAD}}==
{{trans|C}}
<syntaxhighlight lang=MAD"mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(N)
Line 2,877 ⟶ 2,853:
33550336 IS A PERFECT NUMBER.
THERE ARE 5 PERFECT NUMBERS BELOW 35000000</pre>
 
=={{header|Maple}}==
 
{{incorrect|Maple| <br><br> The output for Chowla(1) is incorrect. <br><br> }}
 
<syntaxhighlight lang=Maple"maple">ChowlaFunction := n -> NumberTheory:-SumOfDivisors(n) - n - 1;
 
PrintChowla := proc(n::posint) local i;
Line 2,958 ⟶ 2,933:
664579
[6, 28, 496, 8128, 33550336]</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ClearAll[Chowla]
Chowla[0 | 1] := 0
Chowla[n_] := DivisorSigma[1, n] - 1 - n
Line 2,980 ⟶ 2,954:
664579
{1, 6, 28, 496, 8128, 33550336}</pre>
 
=={{header|Nim}}==
{{trans|C}}
<syntaxhighlight lang="nim">import strformat
import strutils
 
Line 3,078 ⟶ 3,051:
There are 5 perfect numbers < 350,000,000
</pre>
 
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
Line 3,085 ⟶ 3,056:
{{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.
<syntaxhighlight lang="pascal">program Chowla_numbers;
 
{$IFDEF FPC}
Line 3,246 ⟶ 3,217:
real 1m54,534s
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'divisor_sum';
Line 3,333 ⟶ 3,303:
 
There are 5 perfect numbers up to 35,000,000: 6 28 496 8,128 33,550,336</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"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,410 ⟶ 3,379:
become 4s and 90s respectively, without finding anything else. Obviously 1.4e11 and 2.4e18
were picked to minimise the run times.
 
=={{header|Picat}}==
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang=Picat"picat">
table
chowla(1) = 0.
Line 3,507 ⟶ 3,475:
There are 5 perfect numbers less than 35000000.
</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de accu1 (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 3,621 ⟶ 3,588:
(6 28 496 8128 33550336)
</pre>
 
=={{header|PowerBASIC}}==
 
Line 3,627 ⟶ 3,593:
 
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 3,779 ⟶ 3,745:
There are 8 perfect numbers <= 2,305,843,009,213,693,950
press any key to exit program</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang=Prolog"prolog">
chowla(1, 0).
chowla(2, 0).
Line 3,900 ⟶ 3,865:
There are 5 perfect numbers < 35,000,000.
</pre>
 
=={{header|Python}}==
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.
 
<syntaxhighlight lang="python"># https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
from sympy import divisors
 
Line 4,001 ⟶ 3,965:
* Splitting one function for the jit compiler to digest.
 
<syntaxhighlight lang="python">from numba import jit
 
# https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.factor_.divisors
Line 4,040 ⟶ 4,004:
 
Speedup - not much, subjectively...
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(require racket/fixnum)
Line 4,135 ⟶ 4,098:
Primes < 10000000: 664579
There are 5 perfect numbers <= 35000000: (6 28 496 8128 33550336)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 4,144 ⟶ 4,106:
(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" line>sub comma { $^i.flip.comb(3).join(',').flip }
 
sub schnitzel (\Radda, \radDA = 0) {
Line 4,226 ⟶ 4,188:
33,550,336
</pre>
 
=={{header|REXX}}==
<syntaxhighlight 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,333 ⟶ 4,294:
33,550,336 is a perfect number.
</pre>
 
=={{header|Ruby}}==
{{trans|C}}
<syntaxhighlight lang="ruby">def chowla(n)
sum = 0
i = 2
Line 4,439 ⟶ 4,399:
33550336 is a perfect number
There are 5 perfect numbers < 350000000</pre>
 
=={{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.
<syntaxhighlight lang="scala">object ChowlaNumbers {
def main(args: Array[String]): Unit = {
println("Chowla Numbers...")
Line 4,512 ⟶ 4,471:
4: 8,128
5: 33,550,336</pre>
 
=={{header|Swift}}==
 
Uses Grand Central Dispatch to perform concurrent prime counting and perfect number searching
 
<syntaxhighlight lang="swift">import Foundation
 
@inlinable
Line 4,652 ⟶ 4,610:
8128 is a perfect number
33550336 is a perfect number</pre>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|6}}
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="vb">Option Explicit
 
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Line 4,818 ⟶ 4,775:
There are 5 perfect numbers <= 35.000.000
press enter to quit program.</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|Go}}
<syntaxhighlight lang="vbnet">Imports System
 
Module Program
Line 4,923 ⟶ 4,879:
===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.
<syntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Program
Line 5,035 ⟶ 4,991:
2,305,843,008,139,952,128 is a number that is perfect
There are 8 perfect numbers <= 25,000,000,000,000,000,000</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">fn chowla(n int) int {
if n < 1 {
panic("argument must be a positive integer")
Line 5,160 ⟶ 5,115:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascript">import "./fmt" for Fmt
import "./math" for Int, Nums
 
Line 5,253 ⟶ 5,207:
There are 5 perfect numbers <= 35,000,000
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">func Chowla(N); \Return sum of divisors
int N, Div, Sum, Quot;
[Div:= 2; Sum:= 0;
Line 5,344 ⟶ 5,297:
33550336
</pre>
 
=={{header|zkl}}==
{{trans|Go}}
<syntaxhighlight lang="zkl">fcn chowla(n){
if(n<1)
throw(Exception.ValueError("Chowla function argument must be positive"));
Line 5,371 ⟶ 5,323:
c
}</syntaxhighlight>
<syntaxhighlight lang="zkl">fcn testChowla{
println("The first 37 Chowla numbers:\n",
[1..37].apply(chowla).concat(" ","[","]"), "\n");
10,327

edits