Sequence: smallest number greater than previous term with exactly n divisors: Difference between revisions
(Added Java) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add another interpretation of the task) |
||
Line 199: | Line 199: | ||
This task could be interpreted in a few ways. |
This task could be interpreted in a few ways. |
||
Could be the sequence of the '''smallest |
Could be the sequence of the '''smallest natural numbers''' such that each <strong>a<sub>n</sub></strong> has '''n''' divisors: [[oeis:A005179|OEIS: A005179]]. |
||
Or, could be the sequence |
Or, could be the sequence where each term <strong>a<sub>n</sub></strong> is the <strong>smallest natural number > a<sub>n-1</sub></strong> that has '''n''' divisors: [[oeis:A069654|OEIS: A069654]]. |
||
Or, it could be something else entirely. |
|||
⚫ | |||
⚫ | |||
<lang perl6>sub div-count (\x) { |
<lang perl6>sub div-count (\x) { |
||
Line 211: | Line 213: | ||
} |
} |
||
} |
} |
||
put 'First 15 terms of OEIS: A005179'; |
put 'First 15 terms of OEIS: A005179'; |
||
Line 218: | Line 219: | ||
my $m = 1; |
my $m = 1; |
||
put "\nFirst 15 terms of OEIS: A69654"; |
put "\nFirst 15 terms of OEIS: A69654"; |
||
put (1..15).map: -> $n { my $r = $m = first { $n == .&div-count }, $m..Inf }; |
put (1..15).map: -> $n { my $r = $m = first { $n == .&div-count }, $m..Inf }; |
||
# Actually, since there is no verbiage in the task description about |
|||
# choosing the _smallest_ integer, for each term, this complies with |
|||
# a strict interpretation of the task requirements. |
|||
put "\nTechnically correct is the best kind of correct:"; |
|||
my $antipp = (1..5000).race.classify: { .&div-count }; |
|||
put (1..15).map: { $antipp{$_}.pick };</lang> |
|||
{{out}} |
{{out}} |
||
<pre>First 15 terms of OEIS: A005179 |
<pre>First 15 terms of OEIS: A005179 |
||
Line 224: | Line 232: | ||
First 15 terms of OEIS: A69654 |
First 15 terms of OEIS: A69654 |
||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
||
Technically correct is the best kind of correct: |
|||
1 1669 2209 4627 2401 1348 64 3070 3025 496 1024 2144 4096 1472 1936</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 15:05, 9 April 2019
The Anti-primes Plus sequence are the natural numbers in which each nth term has n divisors, including 1 and itself.
Task
Show the first 15 terms of this sequence.
C
<lang c>#include <stdio.h>
- define MAX 15
int count_divisors(int n) {
int i, count = 0; for (i = 1; i * i <= n; ++i) { if (!(n % i)) { if (i == n / i) count++; else count += 2; } } return count;
}
int main() {
int i, next = 1; printf("The first %d anti-primes plus are:\n", MAX); for (i = 1; next <= MAX; ++i) { if (next == count_divisors(i)) { printf("%d ", i); next++; } } printf("\n"); return 0;
}</lang>
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
C++
<lang cpp>#include <iostream>
- define MAX 15
using namespace std;
int count_divisors(int n) {
int count = 0; for (int i = 1; i * i <= n; ++i) { if (!(n % i)) { if (i == n / i) count++; else count += 2; } } return count;
}
int main() {
cout << "The first " << MAX << " anti-primes plus are:" << endl; for (int i = 1, next = 1; next <= MAX; ++i) { if (next == count_divisors(i)) { cout << i << " "; next++; } } cout << endl; return 0;
}</lang>
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Go
<lang go>package main
import "fmt"
func countDivisors(n int) int {
count := 0 for i := 1; i*i <= n; i++ { if n%i == 0 { if i == n/i { count++ } else { count += 2 } } } return count
}
func main() {
const max = 15 fmt.Println("The first", max, "anti-primes plus are:") for i, next := 1, 1; next <= max; i++ { if next == countDivisors(i) { fmt.Printf("%d ", i) next++ } } fmt.Println()
}</lang>
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Java
<lang java>public class AntiPrimesPlus {
static int count_divisors(int n) { int count = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { if (i == n / i) count++; else count += 2; } } return count; }
public static void main(String[] args) { final int max = 15; System.out.printf("The first %d anti-primes plus are:\n", max); for (int i = 1, next = 1; next <= max; ++i) { if (next == count_divisors(i)) { System.out.printf("%d ", i); next++; } } System.out.println(); }
}</lang>
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Kotlin
<lang scala>// Version 1.3.21
const val MAX = 15
fun countDivisors(n: Int): Int {
var count = 0 var i = 1 while (i * i <= n) { if (n % i == 0) { count += if (i == n / i) 1 else 2 } i++ } return count
}
fun main() {
println("The first $MAX anti-primes plus are:") var i = 1 var next = 1 while (next <= MAX) { if (next == countDivisors(i)) { print("$i ") next++ } i++ } println()
}</lang>
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Perl 6
This task could be interpreted in a few ways.
Could be the sequence of the smallest natural numbers such that each an has n divisors: OEIS: A005179.
Or, could be the sequence where each term an is the smallest natural number > an-1 that has n divisors: OEIS: A069654.
Or, it could be something else entirely.
Whichever, here are a few possibilities.
<lang perl6>sub div-count (\x) {
return 2 if x.is-prime; +flat (1 .. x.sqrt.floor).map: -> \d { unless x % d { my \y = x div d; y == d ?? y !! (y, d) } }
}
put 'First 15 terms of OEIS: A005179'; put (1..15).map: -> $n { first { $n == .&div-count }, 1..Inf };
my $m = 1; put "\nFirst 15 terms of OEIS: A69654"; put (1..15).map: -> $n { my $r = $m = first { $n == .&div-count }, $m..Inf };
- Actually, since there is no verbiage in the task description about
- choosing the _smallest_ integer, for each term, this complies with
- a strict interpretation of the task requirements.
put "\nTechnically correct is the best kind of correct:"; my $antipp = (1..5000).race.classify: { .&div-count }; put (1..15).map: { $antipp{$_}.pick };</lang>
- Output:
First 15 terms of OEIS: A005179 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144 First 15 terms of OEIS: A69654 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 Technically correct is the best kind of correct: 1 1669 2209 4627 2401 1348 64 3070 3025 496 1024 2144 4096 1472 1936
Ring
<lang ring>
- Project : ANti-primes
see "working..." + nl see "wait for done..." + nl + nl see "the first 15 Anti-primes Plus are:" + nl + nl num = 1 n = 0 result = list(15) while num < 16
n = n + 1 div = factors(n) if div = num result[num] = n num = num + 1 ok
end see "[" for n = 1 to len(result)
if n < len(result) see string(result[n]) + "," else see string(result[n]) + "]" + nl + nl ok
next see "done..." + nl
func factors(an)
ansum = 2 if an < 2 return(1) ok for nr = 2 to an/2 if an%nr = 0 ansum = ansum+1 ok next return ansum
</lang>
- Output:
working... wait for done... the first 15 Anti-primes Plus are: [1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624] done...
Sidef
a(n) is the smallest number with exactly n divisors (A005179). <lang ruby>func n_divisors(n) {
1..Inf -> first_by { .sigma0 == n }
}
say 15.of { n_divisors(_+1) }</lang>
- Output:
[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]
a(n) is the smallest number > a(n-1) with exactly n divisors (A069654).
<lang ruby>func n_divisors(n, from=1) {
from..Inf -> first_by { .sigma0 == n }
}
with (1) { |from|
say 15.of { from = n_divisors(_+1, from) }
}</lang>
- Output:
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624]