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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Curzon numbers step by step in the Odin 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 Odin programming language

Source code in the odin programming language

package curzon_numbers
/* imports */
import "core:c/libc"
import "core:fmt"
/* main block */
main :: proc() {
	for k: int = 2; k <= 10; k += 2 {
		fmt.println("\nCurzon numbers with base ", k)
		count := 0
		n: int = 1
		for ; count < 50; n += 1 {
			if is_curzon(n, k) {
				count += 1
				libc.printf("%*d ", 4, n)
				if (count) % 10 == 0 {
					fmt.printf("\n")
				}
			}
		}
		for {
			if is_curzon(n, k) {
				count += 1}
			if count == 1000 {
				break}
			n += 1
		}
		libc.printf("1000th Curzon number with base %d: %d \n", k, n)
	}
}
/* definitions */
modpow :: proc(base, exp, mod: int) -> int {
	if mod == 1 {
		return 0}
	result: int = 1
	base := base
	exp := exp
	base %= mod
	for ; exp > 0; exp >>= 1 {
		if ((exp & 1) == 1) {
			result = (result * base) % mod}
		base = (base * base) % mod
	}
	return result
}

is_curzon :: proc(n: int, k: int) -> bool {
	r := k * n //const?
	return modpow(k, n, r + 1) == r
}


  

You may also check:How to resolve the algorithm Range expansion step by step in the Aime programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Sidef programming language
You may also check:How to resolve the algorithm Hilbert curve step by step in the Delphi programming language
You may also check:How to resolve the algorithm String case step by step in the Raven programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Kotlin programming language