Lucky and even lucky numbers: Difference between revisions

Added 11l
(Added 11l)
Line 88:
* Sequence [https://oeis.org/A045954 A045954 even lucky numbers or ELN] on The On-Line Encyclopedia of Integer Sequences.
* Entry [http://mathworld.wolfram.com/LuckyNumber.html lucky numbers] on The Eric Weisstein's World of Mathematics.<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>V NoValue = 0
 
F initLuckyNumbers(nelems, evenlucky, limit = -1)
[Int] result
L(i) 0 .< nelems
V k = i
L(j) (result.len - 1 .< 0).step(-1)
k = k * result[j] I/ (result[j] - 1)
V n = 2 * k + 1 + evenlucky
I limit != -1 & n > limit
L.break
result.append(n)
R result
 
F name(evenlucky)
R [‘Lucky’, ‘Even lucky’][evenlucky]
 
F printSingle(j, evenlucky)
V luckySeq = initLuckyNumbers(j, evenlucky)
print(name(evenlucky)‘ number at index ’j‘ is ’luckySeq[j - 1])
 
F printRange(j, k, evenlucky)
V luckySeq = initLuckyNumbers(k, evenlucky)
print(name(evenlucky)‘ numbers at indexes ’j‘ to ’k‘ are: ’, end' ‘’)
L(idx) j - 1 .< k
I idx != j - 1
print(‘, ’, end' ‘’)
print(luckySeq[idx], end' ‘’)
print()
 
F printInRange(j, k, evenlucky)
V luckySeq = initLuckyNumbers(k, evenlucky, k)
print(name(evenlucky)‘ numbers between ’j‘ to ’k‘ are: ’, end' ‘’)
V first = 1B
L(val) luckySeq
I val > j
I first
first = 0B
E
print(‘, ’, end' ‘’)
print(val, end' ‘’)
print()
 
F process_args(args)
assert(args.len C 1..3, ‘Wrong number of arguments’)
 
// First argument: "j" value.
V j = Int(args[0])
 
V k = NoValue
// Second argument: "k" value or a comma.
I args.len > 1
I args[1] == ‘,’
// Must be followed by the kind of lucky number.
assert(args.len == 3, ‘Missing kind argument’)
E
k = Int(args[1])
assert(k != 0, ‘Expected a non null number’)
 
V evenlucky = 0
// Third argument: number kind.
I args.len == 3
V kind = args[2].lowercase()
assert(kind C (‘lucky’, ‘evenlucky’), ‘Wrong kind’)
I kind == ‘evenlucky’
evenlucky = 1
 
I k == NoValue
// Print jth value.
printSingle(j, evenlucky)
E I k > 0
// Print jth to kth values.
printRange(j, k, evenlucky)
E
// Print values in range j..(-k).
printInRange(j, -k, evenlucky)
 
:start:
I 1B
L(args) [‘1 20’, ‘1 20 evenlucky’, ‘6000 -6100’, ‘6000 -6100 evenlucky’, ‘10000’, ‘10000 , lucky’, ‘10000 , evenlucky’]
print(‘Command line arguments: ’args)
process_args(args.split(‘ ’))
print()
E
process_args(:argv[1..])</lang>
 
{{out}}
<pre>
Command line arguments: 1 20
Lucky numbers at indexes 1 to 20 are: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79
 
Command line arguments: 1 20 evenlucky
Even lucky numbers at indexes 1 to 20 are: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76
 
Command line arguments: 6000 -6100
Lucky numbers between 6000 to 6100 are: 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093
 
Command line arguments: 6000 -6100 evenlucky
Even lucky numbers between 6000 to 6100 are: 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092
 
Command line arguments: 10000
Lucky number at index 10000 is 115591
 
Command line arguments: 10000 , lucky
Lucky number at index 10000 is 115591
 
Command line arguments: 10000 , evenlucky
Even lucky number at index 10000 is 111842
 
</pre>
 
=={{header|C}}==
1,480

edits