Curzon numbers

Revision as of 15:33, 6 March 2022 by Simonjsaunders (talk | contribs) (Added C++ and Rust solutions)

A Curzon number is defined to be a positive integer n for which 2n + 1 is evenly divisible by 2 × n + 1.

Curzon numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Generalized Curzon numbers are those where the positive integer n, using a base integer k, satisfy the condition that kn + 1 is evenly divisible by k × n + 1.

Base here does not imply the radix of the counting system; rather the integer the equation is based on. All calculations should be done in base 10.

Generalized Curzon numbers only exist for even base integers.


Task
  • Find and show the first 50 Generalized Curzon numbers for even base integers from 2 through 10.


Stretch
  • Find and show the one thousandth.


See also

and even though it is not specifically mentioned that they are Curzon numbers:



C++

Library: GMP

<lang cpp>#include <iomanip>

  1. include <iostream>
  2. include <vector>
  1. include <gmpxx.h>

bool is_curzon(int n, int k) {

   mpz_class p;
   mpz_ui_pow_ui(p.get_mpz_t(), k, n);
   return (p + 1) % (k * n + 1) == 0;

}

int main() {

   for (int k = 2; k <= 10; k += 2) {
       std::cout << "Curzon numbers with base " << k << ":\n";
       int count = 0, n = 1;
       for (; count < 50; ++n) {
           if (is_curzon(n, k)) {
               std::cout << std::setw(4) << n
                         << (++count % 10 == 0 ? '\n' : ' ');
           }
       }
       for (;;) {
           if (is_curzon(n, k))
               ++count;
           if (count == 1000)
               break;
           ++n;
       }
       std::cout << "1000th Curzon number with base " << k << ": " << n
                 << "\n\n";
   }
   return 0;

}</lang>

Output:
Curzon numbers with base 2:
   1    2    5    6    9   14   18   21   26   29
  30   33   41   50   53   54   65   69   74   78
  81   86   89   90   98  105  113  114  125  134
 138  141  146  153  158  165  173  174  186  189
 194  198  209  210  221  230  233  245  249  254
1000th Curzon number with base 2: 8646

Curzon numbers with base 4:
   1    3    7    9   13   15   25   27   37   39
  43   45   49   57   67   69   73   79   87   93
  97   99  105  115  127  135  139  153  163  165
 169  175  177  183  189  193  199  205  207  213
 219  235  249  253  255  265  267  273  277  279
1000th Curzon number with base 4: 9375

Curzon numbers with base 6:
   1    6   30   58   70   73   90  101  105  121
 125  146  153  166  170  181  182  185  210  233
 241  242  266  282  290  322  373  381  385  390
 397  441  445  446  450  453  530  557  562  585
 593  601  602  605  606  621  646  653  670  685
1000th Curzon number with base 6: 20717

Curzon numbers with base 8:
   1   14   35   44   72   74   77  129  131  137
 144  149  150  185  200  219  236  266  284  285
 299  309  336  357  381  386  390  392  402  414
 420  441  455  459  470  479  500  519  527  536
 557  582  600  602  617  639  654  674  696  735
1000th Curzon number with base 8: 22176

Curzon numbers with base 10:
   1    9   10   25  106  145  190  193  238  253
 306  318  349  385  402  462  486  526  610  649
 658  678  733  762  810  990  994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
1000th Curzon number with base 10: 46845

Julia

<lang julia>isCurzon(n, k) = (BigInt(k)^n + 1) % (k * n + 1) == 0

function printcurzons(klow, khigh)

   for k in filter(iseven, klow:khigh)
       n, curzons = 0, Int[]
       while length(curzons) < 1000
           isCurzon(n, k) && push!(curzons, n)
           n += 1
       end
       println("Curzon numbers with k = $k:")
       foreach(p -> print(lpad(p[2], 5), p[1] % 25 == 0 ? "\n" : ""), enumerate(curzons[1:50]))
       println("    Thousandth Curzon with k = $k: ", curzons[1000])
   end

end

printcurzons(2, 10)

</lang>

Output:
Curzon numbers with k = 2:
    1    2    5    6    9   14   18   21   26   29   30   33   41   50   53   54   65   69   74   78   81   86   89   90   98
  105  113  114  125  134  138  141  146  153  158  165  173  174  186  189  194  198  209  210  221  230  233  245  249  254
    Thousandth Curzon with k = 2: 8646
Curzon numbers with k = 4:
    1    3    7    9   13   15   25   27   37   39   43   45   49   57   67   69   73   79   87   93   97   99  105  115  127
  135  139  153  163  165  169  175  177  183  189  193  199  205  207  213  219  235  249  253  255  265  267  273  277  279
    Thousandth Curzon with k = 4: 9375
Curzon numbers with k = 6:
    1    6   30   58   70   73   90  101  105  121  125  146  153  166  170  181  182  185  210  233  241  242  266  282  290
  322  373  381  385  390  397  441  445  446  450  453  530  557  562  585  593  601  602  605  606  621  646  653  670  685
    Thousandth Curzon with k = 6: 20717
Curzon numbers with k = 8:
    1   14   35   44   72   74   77  129  131  137  144  149  150  185  200  219  236  266  284  285  299  309  336  357  381
  386  390  392  402  414  420  441  455  459  470  479  500  519  527  536  557  582  600  602  617  639  654  674  696  735
    Thousandth Curzon with k = 8: 22176
Curzon numbers with k = 10:
    1    9   10   25  106  145  190  193  238  253  306  318  349  385  402  462  486  526  610  649  658  678  733  762  810
  990  994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
    Thousandth Curzon with k = 10: 46845

Perl

<lang perl>use strict; use warnings; use Math::AnyNum 'ipow';

sub curzon {

   my($base,$cnt) = @_;
   my($n,@C) = 0;
   while (++$n) {
       push @C, $n if 0 == (ipow($base,$n) + 1) % ($base * $n + 1);
       return @C if $cnt == @C;
   }

}

my $upto = 50; for my $k (<2 4 6 8 10>) {

   my @C = curzon($k,1000);
   print "First $upto Curzon numbers using a base of $k:\n" .
   (sprintf "@{['%5d' x $upto]}", @C[0..$upto-1]) =~ s/(.{125})/$1\n/gr;
   print "Thousandth: $C[-1]\n\n";

}</lang>

Output:
First 50 Curzon numbers using a base of 2:
   1    2    5    6    9   14   18   21   26   29   30   33   41   50   53   54   65   69   74   78   81   86   89   90   98
 105  113  114  125  134  138  141  146  153  158  165  173  174  186  189  194  198  209  210  221  230  233  245  249  254
Thousandth: 8646

First 50 Curzon numbers using a base of 4:
   1    3    7    9   13   15   25   27   37   39   43   45   49   57   67   69   73   79   87   93   97   99  105  115  127
 135  139  153  163  165  169  175  177  183  189  193  199  205  207  213  219  235  249  253  255  265  267  273  277  279
Thousandth: 9375

First 50 Curzon numbers using a base of 6:
   1    6   30   58   70   73   90  101  105  121  125  146  153  166  170  181  182  185  210  233  241  242  266  282  290
 322  373  381  385  390  397  441  445  446  450  453  530  557  562  585  593  601  602  605  606  621  646  653  670  685
Thousandth: 20717

First 50 Curzon numbers using a base of 8:
   1   14   35   44   72   74   77  129  131  137  144  149  150  185  200  219  236  266  284  285  299  309  336  357  381
 386  390  392  402  414  420  441  455  459  470  479  500  519  527  536  557  582  600  602  617  639  654  674  696  735
Thousandth: 22176

First 50 Curzon numbers using a base of 10:
   1    9   10   25  106  145  190  193  238  253  306  318  349  385  402  462  486  526  610  649  658  678  733  762  810
 990  994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
Thousandth: 46845

Phix

with javascript_semantics
include mpfr.e
mpz {pow,z} = mpz_inits(2)
for base=2 to 10 by 2 do
    printf(1,"The first 50 Curzon numbers using a base of %d:\n",base)
    integer count = 0, n = 1
    mpz_set_si(pow,base)
    while true do
        mpz_add_ui(z,pow,1)
        integer d = base*n + 1
        if mpz_divisible_ui_p(z,d) then
            count = count + 1
            if count<=50 then
                printf(1,"%5d%s",{n,iff(remainder(count,25)=0?"\n":"")})
            elsif count=1000 then
                printf(1,"One thousandth: %d\n\n",n)
                exit
            end if
        end if
        n += 1
        mpz_mul_si(pow,pow,base)
    end while
end for
Output:
The first 50 Curzon numbers using a base of 2:
    1    2    5    6    9   14   18   21   26   29   30   33   41   50   53   54   65   69   74   78   81   86   89   90   98
  105  113  114  125  134  138  141  146  153  158  165  173  174  186  189  194  198  209  210  221  230  233  245  249  254
One thousandth: 8646

The first 50 Curzon numbers using a base of 4:
    1    3    7    9   13   15   25   27   37   39   43   45   49   57   67   69   73   79   87   93   97   99  105  115  127
  135  139  153  163  165  169  175  177  183  189  193  199  205  207  213  219  235  249  253  255  265  267  273  277  279
One thousandth: 9375

The first 50 Curzon numbers using a base of 6:
    1    6   30   58   70   73   90  101  105  121  125  146  153  166  170  181  182  185  210  233  241  242  266  282  290
  322  373  381  385  390  397  441  445  446  450  453  530  557  562  585  593  601  602  605  606  621  646  653  670  685
One thousandth: 20717

The first 50 Curzon numbers using a base of 8:
    1   14   35   44   72   74   77  129  131  137  144  149  150  185  200  219  236  266  284  285  299  309  336  357  381
  386  390  392  402  414  420  441  455  459  470  479  500  519  527  536  557  582  600  602  617  639  654  674  696  735
One thousandth: 22176

The first 50 Curzon numbers using a base of 10:
    1    9   10   25  106  145  190  193  238  253  306  318  349  385  402  462  486  526  610  649  658  678  733  762  810
  990  994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
One thousandth: 46845

Raku

<lang perl6>sub curzon ($base) { lazy (1..∞).hyper.map: { $_ if (exp($_, $base) + 1) %% ($base × $_ + 1) } };

for <2 4 6 8 10> {

   my $curzon = .&curzon;
   say "\nFirst 50 Curzon numbers using a base of $_:\n" ~
     $curzon[^50].batch(25)».fmt("%4s").join("\n") ~
     "\nOne thousandth: " ~ $curzon[999]

}</lang>

Output:
First 50 Curzon numbers using a base of 2:
   1    2    5    6    9   14   18   21   26   29   30   33   41   50   53   54   65   69   74   78   81   86   89   90   98
 105  113  114  125  134  138  141  146  153  158  165  173  174  186  189  194  198  209  210  221  230  233  245  249  254
One thousandth: 8646

First 50 Curzon numbers using a base of 4:
   1    3    7    9   13   15   25   27   37   39   43   45   49   57   67   69   73   79   87   93   97   99  105  115  127
 135  139  153  163  165  169  175  177  183  189  193  199  205  207  213  219  235  249  253  255  265  267  273  277  279
One thousandth: 9375

First 50 Curzon numbers using a base of 6:
   1    6   30   58   70   73   90  101  105  121  125  146  153  166  170  181  182  185  210  233  241  242  266  282  290
 322  373  381  385  390  397  441  445  446  450  453  530  557  562  585  593  601  602  605  606  621  646  653  670  685
One thousandth: 20717

First 50 Curzon numbers using a base of 8:
   1   14   35   44   72   74   77  129  131  137  144  149  150  185  200  219  236  266  284  285  299  309  336  357  381
 386  390  392  402  414  420  441  455  459  470  479  500  519  527  536  557  582  600  602  617  639  654  674  696  735
One thousandth: 22176

First 50 Curzon numbers using a base of 10:
   1    9   10   25  106  145  190  193  238  253  306  318  349  385  402  462  486  526  610  649  658  678  733  762  810
 990  994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
One thousandth: 46845

Rust

<lang rust>// [dependencies] // rug = "1.15.0"

fn is_curzon(n: u32, k: u32) -> bool {

   use rug::{Complete, Integer};
   (Integer::u_pow_u(k, n).complete() + 1) % (k * n + 1) == 0

}

fn main() {

   for k in (2..=10).step_by(2) {
       println!("Curzon numbers with base {k}:");
       let mut count = 0;
       let mut n = 1;
       while count < 50 {
           if is_curzon(n, k) {
               count += 1;
               print!("{:4}{}", n, if count % 10 == 0 { "\n" } else { " " });
           }
           n += 1;
       }
       loop {
           if is_curzon(n, k) {
               count += 1;
               if count == 1000 {
                   break;
               }
           }
           n += 1;
       }
       println!("1000th Curzon number with base {k}: {n}\n");
   }

}</lang>

Output:
Curzon numbers with base 2:
   1    2    5    6    9   14   18   21   26   29
  30   33   41   50   53   54   65   69   74   78
  81   86   89   90   98  105  113  114  125  134
 138  141  146  153  158  165  173  174  186  189
 194  198  209  210  221  230  233  245  249  254
1000th Curzon number with base 2: 8646

Curzon numbers with base 4:
   1    3    7    9   13   15   25   27   37   39
  43   45   49   57   67   69   73   79   87   93
  97   99  105  115  127  135  139  153  163  165
 169  175  177  183  189  193  199  205  207  213
 219  235  249  253  255  265  267  273  277  279
1000th Curzon number with base 4: 9375

Curzon numbers with base 6:
   1    6   30   58   70   73   90  101  105  121
 125  146  153  166  170  181  182  185  210  233
 241  242  266  282  290  322  373  381  385  390
 397  441  445  446  450  453  530  557  562  585
 593  601  602  605  606  621  646  653  670  685
1000th Curzon number with base 6: 20717

Curzon numbers with base 8:
   1   14   35   44   72   74   77  129  131  137
 144  149  150  185  200  219  236  266  284  285
 299  309  336  357  381  386  390  392  402  414
 420  441  455  459  470  479  500  519  527  536
 557  582  600  602  617  639  654  674  696  735
1000th Curzon number with base 8: 22176

Curzon numbers with base 10:
   1    9   10   25  106  145  190  193  238  253
 306  318  349  385  402  462  486  526  610  649
 658  678  733  762  810  990  994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
1000th Curzon number with base 10: 46845

Wren

Library: Wren-gmp
Library: Wren-seq
Library: Wren-fmt

<lang ecmascript>/* curzon_numbers.wren */

import "./gmp" for Mpz import "./seq" for Lst import "./fmt" for Fmt

for (k in [2, 4, 6, 8, 10]) {

   System.print("The first 50 Curzon numbers using a base of %(k):")
   var count = 0
   var n = 1
   var pow = Mpz.from(k)
   var curzon50 = []
   while (true) {
       var z = pow + Mpz.one
       var d = k*n + 1
       if (z.isDivisibleUi(d)) {
           if (count < 50) curzon50.add(n)
           count = count + 1
           if (count == 50) {
               for (chunk in Lst.chunks(curzon50, 10)) Fmt.print("$4d", chunk)
               System.write("\nOne thousandth: ")
           }
           if (count == 1000) {
               System.print(n)
               break
           }
       }
       n = n + 1
       pow.mul(k)
   }
   System.print()

}</lang>

Output:
The first 50 Curzon numbers using a base of 2:
   1    2    5    6    9   14   18   21   26   29
  30   33   41   50   53   54   65   69   74   78
  81   86   89   90   98  105  113  114  125  134
 138  141  146  153  158  165  173  174  186  189
 194  198  209  210  221  230  233  245  249  254

One thousandth: 8646

The first 50 Curzon numbers using a base of 4:
   1    3    7    9   13   15   25   27   37   39
  43   45   49   57   67   69   73   79   87   93
  97   99  105  115  127  135  139  153  163  165
 169  175  177  183  189  193  199  205  207  213
 219  235  249  253  255  265  267  273  277  279

One thousandth: 9375

The first 50 Curzon numbers using a base of 6:
   1    6   30   58   70   73   90  101  105  121
 125  146  153  166  170  181  182  185  210  233
 241  242  266  282  290  322  373  381  385  390
 397  441  445  446  450  453  530  557  562  585
 593  601  602  605  606  621  646  653  670  685

One thousandth: 20717

The first 50 Curzon numbers using a base of 8:
   1   14   35   44   72   74   77  129  131  137
 144  149  150  185  200  219  236  266  284  285
 299  309  336  357  381  386  390  392  402  414
 420  441  455  459  470  479  500  519  527  536
 557  582  600  602  617  639  654  674  696  735

One thousandth: 22176

The first 50 Curzon numbers using a base of 10:
   1    9   10   25  106  145  190  193  238  253
 306  318  349  385  402  462  486  526  610  649
 658  678  733  762  810  990  994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837

One thousandth: 46845