New answers tagged sequence
0
lst = []
print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")
print("ENTER LIST ELEMENTS :: ")
while True:
n = int(input())
if n == 0 :
print("!!!!!!!!!!! EXIT !!!!!!!!!!!!")
break
else :
lst.append(n)
oddTup = []
evenTup = []
for j in lst :
if j % 2 == 0 :
evenTup. append(j)
else :
...
0
350. Husk, 166 bytes, A000774
*Π¹→ṁ\ḣ
" Explanation
"
" *Π¹→ṁ\ḣ
" → 1 plus
" ṁ\ sum of reciprocals of
" ḣ the range [1..N]
" * times
" Π¹ the factorial of N
Try it online!
Next sequence!
It was surprisingly tricky to get the explanation to come out to the correct length.
2
Barrel, 26 bytes
Disclaimer: The language is newer than the question, but I didn't even think of golfing this until after I'd created the language. I did update the language after I originally wrote the answer, and changed my answer, but that was because I was fixing the interpreter and made some changes to the spec to make the language work better. I wasn't ...
3
349. Kotlin, 774 bytes, A000607
val primes = mutableListOf<Int> (2)
val prev = mutableListOf<Double>(1.0)
fun sequence(n: Int): Double {
if (n < prev.size)
return prev.get(n)
var sum = 0.0
for (k in (primes.last() + 1)..n) {
var prime = true
for (p in primes)
if (k % p == 0) {
...
1
05AB1E, 32 bytes
∞ʒDg6-sηε2.£}¦D€SP.ι3Ý3*>è2ä€ËP*
Try it online! Link includes a header and footer to increase efficiency by making it start looking from \$1,000,000\$, but works (extremely slowly) without them. The program will not terminate after finding all \$900\$ Meeker numbers.
∞ʒDg6-sηε2.£}¦D€SP.ι3Ý3*>è2ä€ËP* # full program
ʒ ...
1
Jelly, 20 bytes
DḌ,PƊƝFḊm3⁼2/Ạ
ȷ6Ç#Ṫ
A monadic Link accepting \$n\$ that yields the \$n^\text{th}\$ Meeker number
Try it online! Very slow so will probably time out for \$n \gt 36\$.
How?
ȷ6Ç#Ṫ - Main Link: integer, n
ȷ6 - 10^6
# - find the first n integers starting at 10^6 for which:
Ç - call Link 1
Ṫ - tail
DḌ,PƊƝFḊm3⁼2/Ạ - Link 1: ...
0
Python - 11 bytes
print -1 0 1
I would say "Good luck improving this.", but past experience has taught me never to assume: though, this won't be easily beat (for i in range(3), %ding the spaces and %sing the 1s are all overweight, so you can eliminate those.) (Edit 1: -4 due to caird, as is expected)
1
Grok, 13 bytes
}q
{p1+YzP9>!
2
Jelly, 3 bytes
-r1
Try it online!
1r-
Try it online!
-ŒR
Try it online!
1ŒR
Try it online!
Ø-Ż
Try it online!
Ø+Ż
Try it online!
-rN
Try it online!
1rN
Try it online!
2Ż’
Try it online!
3Ḷ’
Try it online!
and a bonus one:
M, 2 bytes
-R
Try it online!
answered 17 hours ago
caird coinheringaahing
31.4k77 gold badges7070 silver badges204204 bronze badges
0
dotcomma, 67 bytes
[[[[[[[[[[.,][.].,][.].,][.].,][.].,][.].,][.].,][.].,][.].,][.].,]
Try it online!
A simple approach which nests [[...][.].,], which adds one to and then pushes each number from one to ten. The "root" of this is [.,] which pushes one.
Explanation:
In dotcomma, dots and commas are the only operators. Their behaviors are extremely ...
1
Perl 5, 58 bytes
/(.)(.)(.(.))(.)/,$1*$2==$3&&$5*$4==$'&&say for 1e6..1e7-1
Try it online!
Outputs the entire list of Meeker numbers.
1
Scala, 103 bytes
Stream from 1 map "".+filter{x=>x.size==7&&x.map(_-48).sliding(4,3).forall{x=>x(0)*x(1)==10*x(2)+x(3)}}
Try it in Scastie!
A pretty straightforward answer. You can treat it like a function giving the nth Meeker number (0-indexed) or as a list of all Meeker numbers.
4
R, 101 91 bytes
`[`=`for`;a[1:9,b[0:9,e[0:9,show(paste0(a,b,if(a*b<10)0,d<-a*b,e,if((f=d%%10*e)<10)0,f))]]]
Try it online!
Inspired by and taking a similar approach to Manish Kundu's answer.
This is a horrible piece of R hackery that redefines square brackets '[]', normally used for indexing, to be a short alias for the for looping function.
The ...
2
R, 76 66 bytes
Edit: -10 bytes thanks to Giuseppe
f=function(b,a,n)`if`(nchar(b)>n,substr(b,n,n),f(paste0(b,a),b,n))
Try it online!
Recursive function: input the starting strings b and a (note reversed order), and the 1-based index to output.
Could be a bit shorter (53 bytes) if inputs are vectors of characters instead of strings.
R, 48 bytes
function(...
1
Pyth, 58 bytes
A(/%Q100T+1/Q100)A(*GH*%*GHT%QT)s[+T/QT*"0"<GTG%QT*"0"<HTH
Try it online!
Same as my Python solution. A assigns the two provided elements to the variables G and H respectively. Q is the input (zero-indexed).
3
C (clang), 107 99 bytes
Saved 8 bytes thanks to the man himself Arnauld!!!
a;b;p;q;f(){for(a=0;9/++a;)for(b=0;b<100;)printf("%d%d%02d%d%02d ",a,p=b/10,p*=a,q=b++%10,p%10*q);}
Try it online!
Prints them all.
4
JavaScript (SpiderMonkey), 65 bytes
for(i=99;i<999;print((e-i+'0'-a*b+e)*~99+a*b%10*e))[a,b,e]=++i+''
Try it online!
For example, when i == 267:
i=267;
[a,b,e]=i+'';
print([a, b, e]); // ["2", "6", "7"]
print(e-i); // -260
print(e-i+'0'); // "-2600"
print(a*b); // 12
print(e-i+'0'-a*b); // -2612
print(e-i+'0'-a*b+...
4
Python 2, 121 115 98 bytes
def f(n,t=10):p,q=n%100/t,1+n/100;r=p*q;s=n%t*(r%t);print`t+n/t`+'0'*(r<t)+`r`+`n%t`+'0'*(s<t)+`s`
Try it online!
On noticing the digits, I observed a pattern and got this constant time solution. Takes n as input (0-indexed) and prints n'th meeker number.
For example, the first 2 digits follow the pattern: 10, 11, 12, ... ...
1
Charcoal, 33 bytes
F…χ¹⁰⁰Fχ⟦⪫⟦ι﹪%02dΠικ﹪%02d×﹪Πιχκ⟧ω
Try it online! Link is to verbose version of code. Prints all 900 Meeker numbers. Explanation:
F…χ¹⁰⁰
Loop over all possible first pairs of digits.
Fχ
Loop over all possible fifth digits.
⟦⪫⟦...⟧ω
Print the following items on one line and then move to the next line.
ι
The first two digits.
﹪%02dΠι
...
2
Haskell, 82 80 76 bytes
-6 bytes thanks to kops, for rearranging things and proposing a better interpretation of the rules :P
[n|n<-show<$>[1..],[a,b,c,d,e,f,g]<-[read.pure<$>n],a*b==10*c+d,d*e==10*f+g]
Try it online!
The list of all Meeker numbers, represented as strings.
Haskell, 80 bytes
[n>>=show|n@[a,b,c,d,e,f,g]<-mapM([0.....
5
APL (Dyalog Extended), 45 bytes (SBCS)
Full program. Prints all meeker numbers. Requires 0-based indexing (⎕IO←0).
1e6+⍸{(×⌿i⊇⍵)≡10⊥⍵[2+i←0 1,⍪3 4]}¨10⊤¨1e6…1e7
Try it online! (limited to upper bound of 2 000 000 ― above code works offline)
1e6…1e7 numbers 1 000 000 through 10 000 000
10⊤¨ base-10 representation of each (splits digits of numbers into lists ...
1
Java (JDK), 70 bytes
(a,b,n)->{for(var t=a;b.length()<=n;a=b,b=t)t=b+a;return b.charAt(n);}
Try it online!
3
Husk, 27 bytes
föΛo§=oΠ←od→C2§e←→X4dfo=7LN
Try it online!
returns the list of all meeker numbers. It's horribly inefficient, so here's a version which starts from 1e7, and shows the first n values.
EDIT: corrected the answer after Dominic Van Essen found a bug.
3
JavaScript (Node.js), 86 bytes
_=>[...Array(1e7).keys()].filter(x=>(s=x+'')[0]*s[1]==s[2]+s[3]&&s[3]*s[4]==s[5]+s[6])
Try it online!
I feel like this is too long.
Explanation
Basically, this gets all numbers from 0 to 9999999, coerces each to a string and checks character-wise.
One of Javascript's quirks is that multiplied strings are coerced ...
3
R, 96 92 84 bytes
function(A,B,I,`!`=function(k)"if"(k,"if"(k>1,paste0(!k-1,!k-2),B),A))substr(!I,I,I)
Try it online!
Takes I 1-indexed.
-8 bytes thanks to @Dominic
1
Arn -al, 9 bytes
I'm not particularly sure why this works...
w®¦€•3=⁺■
Try it! 1-indexed, outputs the nth term
Explained
Unpacked: 1 1 1{#+}->
Exploits Arn's weird precedence bugs to save a byte. A more "normal" version would have {_ _+} as the block or something like {#_+} (as # is a suffix)
[ ... ] Implied by `-a` flag
1 First ...
2
Vyxal, d, 5 bytes
5vτ½⌈
Try it Online!
A port of the Jelly answer which is a port of short husk answer.
Explained
5vτ½⌈
5vτ # convert each digit of the input to base 5
½ # halve each item in that list (halving vectorises all the way down)
⌈ # ceiling each item in that list
# -d deep sums the list and implicitly outputs
1
Charcoal, 42 bytes
≔⁻NLζθ≔⁰εW¬‹θ⁰«F¬&ε⊗ε≧⁻⁺Lζ∧﹪ε²Lηθ≦⊕ε»§⁺ηζθ
Try it online! Link is to verbose version of code. Takes the index as the first input. Explanation: I wanted to avoid building up a humunguous string but the code is still slow because I don't know a good way of calculating Fibbinary numbers.
≔⁻NLζθ
Subtract the length of B from n.
≔⁰ε
Start ...
3
Charcoal, 13 bytes
IΣ⭆S⭆↨Iι⁵L↨λ³
Try it online! Link is to verbose version of code. Explanation:
S Convert input to a string
⭆ Map over characters and join
ι Current character
I Cast to integer
↨ ⁵ Convert to base 5
⭆ Map over base 5 digits and join
λ Current ...
2
Retina 0.8.2, 20 15 bytes
.
$*1,
1{5}|11?
Try it online! Link includes test cases. Explanation:
.
$*1,
Convert each digit to unary separately.
1{5}|11?
Count the number of 5s, 2s and 1s needed to make each digit.
4
Python 3 + Z3, N=5
Not 100% sure if this is correct. Basically a standard SAT/SMT coding of the Hamiltonian cycle problem, with additional clauses to block intersecting diagonals and collinear segments. Each time a solution is found all of its distinct rotations/flips are blocked. I've run it up to N=6 (2120 solutions found), which took 30 minutes.
Edit: I ...
3
Python 2, 37 bytes
f=lambda n:n and n/5%2-n%5/-2+f(n/10)
Try it online!
Uses a formula rather than a lookup table for each digit. n/5%2 counts the five-cent coin, and subtracting -n%5/-2 is equivalent to adding (n%5+1)/2 for the one- and two-cent coins.
n%10 0 1 2 3 4 5 6 7 8 9
-----------------------------
n/5%2 0 0 0 0 0 1 1 1 1 1
0-n%5/-2 0 1 ...
4
Wolfram Language (Mathematica), 39 bytes
⌈.4#⌉-⌊#/5⌋&@*IntegerDigits/*Tr
Try it online!
2
05AB1E, 6 bytes
Takes inputs [A, B] and n.
λèì}sè
Try it online!
Commented:
λè } # get the nth element of the sequence generated by:
ì # prepending the current string to the last string
# that starts with [A, B]
s # swap implicit input n to the top of the stack
è # index with n into the nth element of the sequence
1
PowerShell, 63 bytes
param($x,$y,$n)$a=$x,$y
1..$n|%{$a+=$a[$_]+$a[$_-1]}
$a[-1][$n]
Try it online!
-11 bytes thanks to julian
9
Haskell, 15 bytes
a#b=b++b#(a++b)
Try it online!
Takes strings a and b as input, returns the whole infinite Fibonacci word, as is usually allowed in sequence challenges.
How?
Not much to say. This answer relies on the identity
$$
F(a,b)=b+F(b,a+b),
$$
where \$F(a,b)\$ is the infinite Fibonacci word with starting words \$a\$ and \$b\$.
4
Husk, 5 bytes
A rip-off of Delfad0r's beautiful Haskell answer. Go upvote that!
S+S₀+
Try it online!
Returns the entire infinite string/list.
S+S₀+ a b is (S+) ((S₀) (+a)) b, which expands to (+b) ((S₀ (+a)) b) (where ₀ is a self-reference to the main function) and then to (+b) (₀ b (+a b)), which is basically b + F(b, a + b).
Safer answer, 8 bytes
!₁
S+S₁+
...
8
Jelly, 6 5 bytes
⁹;¡⁵ị
Try it online!
Uses 1 indexing
-1 byte thanks to Jonathan Allan, noticing that we could avoid ⁵ becoming the right argument to ;¡ by forcing ;¡ into a nilad-dyad pair with ⁹!
Dyadic ¡ is essentially Jelly's generalised Fibonacci operator
How it works
⁹;¡⁵ị - Main link. Takes A on the left, B on the right and I as the third argument
⁹ ...
8
Python 3, 36 bytes
f=lambda a,b,i:b[i:i+1]or f(b,b+a,i)
Try it online!
-8 bytes thanks to dingledooper
Not a particularly creative approach. In fact, basically this is just what l4m2 did but Python will error when accessing out of bounds instead of returning undefined. Using b[i:i+1] returns b[i] (for strings) if it's in range, but doesn't error and ...
7
JavaScript (Node.js), 26 bytes
n=>g=a=>b=>b[n]||g(b)(b+a)
Try it online!
Thank tsh for -1 Byte
3
Python 2, 48 bytes
f=lambda x:x and int("0112212233"[x%10])+f(x/10)
Try it online!
49 bytes in Python 3 because you'd need // for floor division.
12
Husk, 11 9 8 bytes
Edit: -1 byte thanks to caird coinheringaahing
ṁo⌈½ṁB5d
Try it online!
d # get the digits
ṁB5 # convert them all to base-5
# (this gives a 1 for each 5-denomination coin needed,
# as well as the leftover for each digit.
# We'll need 2 more coins for those with leftover 3 or 4,
# ...
5
05AB1E, 11 bytes
S<•δ¬Èº•sèO
Try it online!
Same approach as my Jelly answer.
How it works
S<•δ¬Èº•sèO - Program. Input: n
S - Cast n to digits
< - Decrement
•δ¬Èº• - Compressed integer: 1122122330
sè - Using n's digits, index into the digits of 1122122330
O - Sum
Kudos to Kevin Cruijssen's excellent ...
8
R, 54 51 47 45 bytes
Edit: converted to console input instead of a full function to try not to fall behind Robin Ryder's answer
d=utf8ToInt(scan(,''))-48;sum(d>0,d>5,d%%5>2)
Try it online!
13
JavaScript (ES7), 36 35 bytes
Similar to other answers. Using a Black Magic formula instead of a lookup table.
f=n=>n&&(n%10)**29%3571%4+f(n/10|0)
Try it online!
Here is a script that looks for \$(p,m)\$ pairs such that \$(n^p\bmod m)\bmod 4=a_n\$ for all \$n\in[0..9]\$.
It's worth noting that this code takes IEEE-754 precision errors into ...
3
C (gcc), 39 bytes
f(n){n=n?f(n/10)+""[n%10]:0;}
Try it online!
JavaScript (Node.js), 37 bytes
f=n=>n&&+"0112212233"[n%10]+f(n/10|0)
Try it online!
9
Haskell, 55…41 40 bytes
-1 byte thanks to xnor, for using a string instead of the hard-coded list.
a=0:tail[i+read[j]|i<-a,j<-"0112212233"]
Try it online!
a is the infinite sequence.
How?
It's not hard to find the recursive formula
$$
a(n)=a\left(\left\lfloor\frac{n}{10}\right\rfloor\right)+a(n \operatorname{mod} 10)
$$
with the base cases ...
4
Husk, 23 bytes
L◄Lfo=⁰Σ↑o≤⁰LṖƒ(+İ€m*10
Try it online!
Extremely slow past 11.
Explanation
L◄Lfo=⁰Σ↑o≤⁰LṖƒ(+İ€m*10
ƒ( create an infinite list using:
İ€ currency denomination builtin: [1,1/2,1/5,...200]
+ plus
m*10 the input mapped to *10
this gives [1,1/...
7
R, 64 52 50 45 bytes
sum(c(1,2,1:3)[.6*utf8ToInt(scan(,""))-28.4])
Try it online!
Same strategy as Delfad0r's Haskell answer, which is nicely explained.
First, scan(,"") reads in input as a string. Then, utf8ToInt(...)-48 takes a string of digits and converts it to a vector of integer digits. This works out shorter than taking input as ...
5
Jelly, 7 bytes
Db5FHĊS
Try it online!
Steals Ports Dominic Van Essen's Husk answer, be sure to upvote that!
How it works
Db5FHĊS - Main link. Takes an integer n on the left
D - Convert to digits
b5 - Convert each digit to base 5
F - Flatten
H - Halve each
Ċ - Ceiling of each
S - SUm
Jelly, 11 bytes
Dị“FȮŀO’D¤S
Try it ...
Top 50 recent answers are included
Related Tags
sequence × 750code-golf × 686
number × 215
math × 187
number-theory × 93
integer × 68
arithmetic × 56
combinatorics × 56
primes × 56
fibonacci × 39
string × 31
binary × 31
code-challenge × 28
cops-and-robbers × 28
subsequence × 27
base-conversion × 23
decision-problem × 22
kolmogorov-complexity × 19
array-manipulation × 18
fastest-code × 17
geometry × 16
permutations × 13
palindrome × 11
ascii-art × 10
restricted-source × 10