Skip to content

Commit cabe237

Browse files
committed
new file: Math/tower_of_distinct_powers.sf
new file: Math/tower_of_powers_inverse.sf
1 parent 8393ead commit cabe237

8 files changed

+96
-22
lines changed

Math/both_truncatable_primes_in_base.sf

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
# https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
2424
# https://oeis.org/A323396 - Irregular array read by rows, where T(n, k) is the k-th prime that is both left-truncatable and right-truncatable in base n.
2525

26-
func digits2num(digits, base) {
27-
digits.sum_kv {|k,v| base**k * v }
28-
}
29-
3026
func is_left_truncatable_prime(digits, base) {
3127

3228
for k in (^digits) {
33-
is_prime(digits2num([digits[0..k]], base)) || return false
29+
base.digits2num(digits.first(k+1)).is_prime || return false
3430
}
3531

3632
return true
@@ -59,7 +55,7 @@ func both_truncatable_primes(base) { # finite sequence for each base
5955
.sort
6056
}
6157

62-
for base in (3..11) {
58+
for base in (3..15) {
6359
var a = both_truncatable_primes(base)
6460
say "There are #{'%3d' % a.len} both-truncatable primes in base #{'%2d' % base} where largest is #{a[-1]}"
6561
}

Math/esthetic_numbers.sf

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ func generate_esthetic(root, upto, callback, b=10) {
1818

1919
var v = root.digits2num(b)
2020

21-
if (v > upto) {
22-
return nil
23-
}
24-
21+
return nil if (v > upto)
2522
callback(v)
2623

2724
var t = root.head

Math/is_both_truncatable_prime.sf

-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
# https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
2626
# https://oeis.org/A323396 - Irregular array read by rows, where T(n, k) is the k-th prime that is both left-truncatable and right-truncatable in base n.
2727

28-
func digits2num(arr, base) {
29-
arr.sum_kv {|k,v| base**k * v }
30-
}
31-
3228
func is_left_truncatable_prime(n, base) {
3329

3430
var digits = n.digits(base)

Math/left_truncatable_primes_in_base.sf

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
# https://en.wikipedia.org/wiki/Truncatable_prime
1010
# https://rosettacode.org/wiki/Find_largest_left_truncatable_prime_in_a_given_base
1111

12-
func digits2num(digits, base) {
13-
digits.map_kv {|k,v| base**k * v }.sum
14-
}
15-
1612
func generate_from_suffix(p, base) {
1713

1814
var seq = [p]

Math/right_truncatable_primes_in_base.sf

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
# https://www.youtube.com/watch?v=azL5ehbw_24
99
# https://en.wikipedia.org/wiki/Truncatable_prime
1010

11-
func digits2num(digits, base) {
12-
digits.sum_kv {|k,v| base**k * v }
13-
}
14-
1511
func generate_from_prefix(p, base) {
1612

1713
var seq = [p]

Math/tower_of_distinct_powers.sf

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/ruby
2+
3+
# Daniel "Trizen" Șuteu
4+
# Date: 05 June 2020
5+
# https://github.com/trizen
6+
7+
# Find a solution to a^b^c^...^(z^z) = n, for a tower of k distinct powers.
8+
9+
func tower_of_powers(n, k) {
10+
gather {
11+
k.times {
12+
take(n = n.lgrt)
13+
}
14+
}
15+
}
16+
17+
var arr = tower_of_powers(2, 10)
18+
19+
with ('a') {|l|
20+
21+
arr.each {|x|
22+
say "#{l++} = #{x}"
23+
}
24+
25+
say "\nResult for a^b^c^...^z:"
26+
say arr.flip.reduce{|a,b|
27+
b**a
28+
}
29+
30+
arr << arr.last
31+
say "\nResult for a^b^c^...^(z^z):"
32+
say arr.flip.reduce{|a,b|
33+
b**a
34+
}
35+
}
36+
37+
__END__
38+
a = 1.55961046946236934997038876876500299328488351184
39+
b = 1.37997039661105766557865852046718024244983839926
40+
c = 1.28486974908322111138116130550302803396637862332
41+
d = 1.22670717265797917836276308964722787277231977304
42+
e = 1.18772296630757250640873514031261013753776638981
43+
f = 1.1598874880354098360292139394176647261367943972
44+
g = 1.13907200407942084565168221813587429120796585878
45+
h = 1.1229478230276636302739154417957596485177010612
46+
i = 1.1101064783478007292988613612539909225461593927
47+
j = 1.09964821208918239074785053448745934624006797166
48+
49+
Result for a^b^c^...^z:
50+
1.99999996313334546345876725053319567243649389716
51+
52+
Result for a^b^c^...^(z^z):
53+
2

Math/tower_of_powers_inverse.sf

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/ruby
2+
3+
# Daniel "Trizen" Șuteu
4+
# Date: 05 June 2020
5+
# https://github.com/trizen
6+
7+
# Find a real solution to x^x^x^...^x = n for a tower of k powers.
8+
9+
func tower_of_powers_inverse(n, k) { # solution for x^x^...^x = n
10+
bsearch_inverse(n, {|v|
11+
k.of(v).reduce{|a,b|
12+
b**a
13+
} <~> n
14+
})
15+
}
16+
17+
func nested_powers_inverse(n, k) { # solution for ((...)^x)^x = n
18+
bsearch_inverse(n, {|v|
19+
k.of(v).reduce{|a,b|
20+
a**b
21+
} <~> n
22+
})
23+
}
24+
25+
# Find x such that x^(x^(x^x)) = 2
26+
# x =1.4466014324298641745973339875976614806873210422...
27+
with (tower_of_powers_inverse(2, 4)) {|x|
28+
say x
29+
assert(x**(x**(x**x)) =~= 2)
30+
}
31+
32+
# Find x such that ((x^x)^x)^x = 2
33+
# x = 1.3367097349494955658483777999091512580783714292...
34+
# Equivalently: x^(x^3) = 2
35+
with (nested_powers_inverse(2, 4)) {|x|
36+
say x
37+
assert(((x**x)**x)**x =~= 2)
38+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ A nice collection of day-to-day Sidef scripts.
452452
* [System of linear equations](./Math/system_of_linear_equations.sf)
453453
* [Tangent numbers](./Math/tangent_numbers.sf)
454454
* [Tonelli shanks algorithm](./Math/tonelli_shanks_algorithm.sf)
455+
* [Tower of distinct powers](./Math/tower_of_distinct_powers.sf)
456+
* [Tower of powers inverse](./Math/tower_of_powers_inverse.sf)
455457
* [Towers of hanoi](./Math/towers_of_hanoi.sf)
456458
* [Trial division with primorials](./Math/trial_division_with_primorials.sf)
457459
* [Tribonacci closed form](./Math/tribonacci_closed_form.sf)

0 commit comments

Comments
 (0)