All SolutionsAll Solutions
🔢
Encoded Digits
Week 25, 2026
Python - isPrime & SubStrings | BMC | Python Solutions
import math
def is_prime(n: int) -> bool:
if n <= 1: return False
if n == 2: return True
if n % 2 == 0: return False
limit = int(math.isqrt(n))
for i in range(3, limit + 1, 2):
if n % i == 0:
return False
return True
def convert(digitString: str) -> int:
ans = []
for start in range(len(digitString)):
for end in range(start+1, len(digitString)+1):
sub = digitString[start:end]
if is_prime(int(sub)):
ans.append(sub)
return "".join(ans)
def check_convert() -> bool:
for d, k in data.items():
if convert(d) != k:
return False
return True
# main
data = {
"123" : "2233",
"1234" : "2233",
"12345" : "22335",
"456" : "5",
"789" : "789",
"13" : "133",
"013" : "013133",
"1" : "",
"11" : "11",
"111" : "1111",
"1111" : "111111",
"11111" : "11111111",
"222" : "222",
"333" : "333",
"444" : "",
"555" : "555",
"666" : "",
"777" : "777",
"888" : "",
"999" : "",
"13579" : "1335779",
"123456789" : "2232345678934567567789",
"3141592653589793" : "331314159141591415926535894141594159265358971592653559592653589726536535897553335897358979558975897958979389977793",
"987654321" : "77654354332"
}
print("CONVERSION WORKING?:",check_convert())
value_to_find = "20266172596384"
print(convert(value_to_find))
Gen substrings, check if prime, append to result | greenya | Odin Solutions
package main
import "core:fmt"
import "core:math"
import "core:strconv"
import "core:strings"
main :: proc () {
digits := "20266172596384"
fmt.printfln("Input:\t%v", digits)
fmt.printfln("Output:\t%v", encode(digits))
}
encode :: proc (digits: string) -> string {
sb := strings.builder_make()
for digits := digits; digits != ""; digits = digits[1:] {
for l in 1..=len(digits) {
n, _ := strconv.parse_int(digits[:l])
if is_prime(n) do strings.write_string(&sb, digits[:l])
}
}
return strings.to_string(sb)
}
is_prime :: proc (n: int) -> bool {
if n<2 do return false
if n==2 || n==3 do return true
if n%2==0 do return false
@static cache: map [int] struct {}
if n in cache do return true
m := int(math.floor(math.sqrt(f64(n))))
for i:=3; i<=m; i+=2 {
if n%i==0 do return false
}
cache[n] = {}
return true
}