How to resolve the algorithm Curzon numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Curzon numbers step by step in the Wren programming language

Table of Contents

Problem Statement

A Curzon number is defined to be a positive integer n for which 2n + 1 is evenly divisible by 2 × n + 1. 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.

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Curzon numbers step by step in the Wren programming language

Source code in the wren programming language

/* Curzon_numbers.wren */

import "./gmp" for Mpz
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) {
                Fmt.tprint("$4d", curzon50, 10)
                System.write("\nOne thousandth: ")
            }
            if (count == 1000) {
                Fmt.print("$,d", n)
                break
            }
        }
        n = n + 1
        pow.mul(k)
    }
    System.print()
}


import "./math" for Int
import "./fmt" for Fmt

var isCurzon = Fn.new { |n, k|
    var r = k * n
    return Int.modPow(k, n, r+1) == r
}

var k = 2
while (k <= 10) {
    System.print("Curzon numbers with base %(k):")
    var n = 1
    var count = 0
    while (count < 50) {
        if (isCurzon.call(n, k)) {
            Fmt.write("$4d ", n)
            count = count + 1
            if (count % 10 == 0) System.print()
        }
        n = n + 1
    }
    while (true) {
        if (isCurzon.call(n, k)) count = count + 1
        if (count == 1000) break
        n = n + 1
    }
    Fmt.print("1,000th Curzon number with base $d: $,d\n", k, n)
    k = k + 2
}


  

You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Rust programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Factor programming language
You may also check:How to resolve the algorithm Arena storage pool step by step in the J programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the R programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Robotic programming language