[Question]
  [
You should write a program or function that receives a positive integer `N` as input and prints out the first `N` characters of your code. If `N` is larger than the length of your code you should continue output your code cyclically.
Reading your source code in any way and reading from file, stdio, etc. are disallowed.
## Examples
(assuming your code is `yourcode`)
Input => Output: 
`5` => `yourc`  
`10` => `yourcodeyo`  
`22` => `yourcodeyourcodeyour`
## Clarification
Your program should be at least 1 byte long.
  
[Answer]
## Python 2, 61 bytes
```
def f(n):a='def f(n):a=%r;print(a%%a*n)[:n]';print(a%a*n)[:n]
```
[Try it online!](https://tio.run/##K6gsycjPM/r/PyU1TSFNI0/TKtFWHYmtWmRdUJSZV6KRqKqaqJWnGW2VF6sOF4KJ/E/TMDTV5AILc6VpmCCxzQw1/wMA)
[Answer]
# [><>](http://esolangs.org/wiki/Fish), 49 bytes
```
'3d*}r0ff+0i:&0(?.~~a*&"0"-+60.&~:?!;1-&:o}&" "0.
```
Half the code is converting the input from a string to an int. If we're allowed to use the code point of a single char read from STDIN instead, then this program would be much shorter at 21 bytes:
```
'3d*}ri:?!;1-&:o}&60.
```
## Explanation
I'll use the second program for the explanation.
`'` starts string parsing, pushing every char until a closing quote is found. Since the rest of the line has no `'` quote, every char except the initial `'` is pushed onto the stack.
But ><> is a toroidal 2D language, so after the line is over the instruction pointer wraps back to the start, hitting the `'` again and *stops* string parsing. The result is that we've pushed everything necessary except the initial quote, namely
```
3d*}ri:0=?;1-&:o}&60.
```
`'` is ASCII 39, so we push the initial quote by pushing `3d* = 3*13 = 39`. We then shift the stack right (`}`) and reverse (`r`), giving:
```
.06&}o:&-1;?=0:ir}*d3'
```
Now we're all set up to start printing. `i` reads in a char of input, but ><> chars are basically integers. In the first program, the `i` is replaced with a loop that converts a digit string from STDIN into an integer.
We then execute the following loop to print out the first N chars:
```
:?!;               If the top of the stack (N) is 0, then terminate
                   Otherwise...
1-                 Subtract 1 from N
&                  Move N to the register temporarily
:o                 Output the next char in the program
}                  Shift the stack right
&                  Put N back on the stack
60.                Repeat loop by jumping back to the first :
```
[Answer]
# CJam, ~~34 17~~ 16 bytes
This can be golfed a lot..
```
{`"_~"+ri_@*<}_~
```
**Code expansion**:
```
{`"_~"+ri_@*<}_~
{            }_~      "Put this code block on stack, take a copy and execute the copy";
 `                    "When executed, this block will the top element on stack to a string";
  "_~"                "Then put this string on stack";
      +               "Concat it to the the previous string on stack which ` created";
       ri             "Read an integer from STDIN";
         _@           "Copy the integer and put the concatinated string on top of stack";
           *          "Repeat the string input number of times";
            <         "Take the first input number of characters from the repeated string";
```
Finally, anything on stack gets printed to STDOUT automatically
[Try it online here](http://cjam.aditsu.net/)
[Answer]
# Python 2, 117 bytes
```
b=input();a=lambda x:(b*(2*(x+chr(34))+')'))[:b];print a("b=input();a=lambda x:(b*(2*(x+chr(34))+')'))[:b];print a(")
```
Life protip: don't execute `list(itertools.cycle(x))`. For some reason, I can't imagine why, it crashes the interpreter.
[Answer]
# JavaScript (ES6), ~~65~~ ~~52~~ ~~50~~ ~~47~~ ~~41~~ 39
```
q=n=>('q='+q).repeat(n/39+1).slice(0,n)
```
Uses ES6 `repeat()` to clone the code, then slices down. Uses a hardcoded length. 
---
Old version (50):
```
q=n=>n>(l=(t='q='+q).length)?t+q(n-l):t.slice(0,n)
```
Creates a function `q`, taking a single parameter.
It stringifies the function text, and recursively calls the function if `n` is greater than the text's length. Otherwise, it returns a substring of the text.
Non ES6 version (65):
```
function q(n){return t=q+'',l=t.length,n>l?t+q(n-l):t.slice(0,n)}
```
[Answer]
# J - 24 char
Takes a single positive integer argument and spits out a string.
```
($],quote)&'($],quote)&'
```
J doesn't have any self-reference tricks, so we just do it the quine way. Explained by explosion:
```
  ],quote                 NB. right arg prepended to quotation of right arg (quine)
 $                        NB. cyclically take left-arg characters from that
(        )&'($],quote)&'  NB. preload right arg with  ($],quote)&
```
The dyadic `$` operator in J cyclically takes items from its right argument to fit the dimensions specified on the left. When the dimension is a single number, this is a simple 1D list of characters, so we do exactly what the question asks.
Try it for yourself at [tryj.tk](https://a296a8f7c5110b2ece73921ddc2abbaaa3c90d10.googledrive.com/host/0B3cbLoy-_9Dbb0NaSk9MRGE5UEU/index.html#code=%28%24%5D%2Cquote%29%26%27%28%24%5D%2Cquote%29%26%27%2030).
[Answer]
# k2 - 7 char
```
{x#$_f}
```
In English, this is a function with argument `x` whose definition is "`x` take string self". 
* *Self* (the noun `_f`) is the innermost currently executing function. Here it is the function `{x#$_f}`.
* *String* (monadic `$`) converts its argument to a string. In the case of a function, it creates a string with the function's original definition.
* *Take* (dyadic `#`) takes *left-arg* items form the list in *right-arg*. In the case of a string, the items are characters, so this is doing exactly what we want.
This *will not* work in the open-source Kona, because it seems to create black holes which eat all attempts to use them as arguments to anything. I am unsure of proper k3 semantics but they are probably not much kinder.
In Q, this is `{x#string .z.s}` and in k4 `{x#2_$.z.s}`. We have to use `2_` to drop two initial characters in k4, for reasons only a mother could love.
[Answer]
# Ruby, ~~66 64~~ 63 bytes
```
eval s=%q(puts"eval s=%q(#{s})".chars.cycle.take(gets.to_i)*'')
```
The same using a function to avoid calling `gets` is a bit longer (81 bytes):
```
def f(i);eval s=%q(puts"def f(i);eval s=%q(#{s});end".chars.cycle.take(i)*'');end
```
Lambda versions of the same are 69 and 65 bytes:
```
l=->i{eval s=%q(puts"l=->i{eval s=%q(#{s})}".chars.cycle.take(i)*'')}
->i{eval s=%q(puts"->i{eval s=%q(#{s})}".chars.cycle.take(i)*'')}
```
[Answer]
# Mathematica, 65 bytes
```
Print[StringTake[StringJoin @@ Table[ToString[#0], {#1}], #1]] & 
```
All spaces is necessary to make this a proper quine, including the trailing one. This is a pure function, which you can use as follows:
```
Print[StringTake[StringJoin @@ Table[ToString[#0], {#1}], #1]] & [75]
```
which prints
```
Print[StringTake[StringJoin @@ Table[ToString[#0], {#1}], #1]] & Print[Stri
```
Unfortunately, applying `ToString` to a function doesn't yield exactly the way you entered the function, so I can't shorten this by removing whitespace, shortening `#1` to `#` or using prefix notation for function calls.
[Answer]
# MATLAB, ~~319~~ 141 characters
I managed to squeeze a few bytes from the original one:
```
function d=g(n);d='gvodujpo!e>h)o*<e>(<e>\e)2;2:*.2-e-e)2:;foe*.2^<e>e)2,npe)1;o.2-252**<';d=[d(1:19)-1,d,d(19:end)-1];d=d(1+mod(0:n-1,141));
```
[Answer]
# JavaScript, 34 bytes
```
f=n=>n&&('f='+f+f(n-1)).slice(0,n)
```
Recursive function that repeats the code `n` times, then slices the result.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 2 bytes
```
îî
```
[Try it online!](https://tio.run/##y0osKPn///C6w@v@/zcGAA "Japt – Try It Online")
The first `î` is a number method that takes one parameter, and repeats it to length `n`. Because it is the first method, `n` becomes the input. The second `î` gets cast into a string, and gets repeated.
This transpiles to:
`n.î("î")` -> Repeat `"î"` until it reaches length `n`
## 8 byte solution
```
îQi"îQi"
```
[Try it online!](https://tio.run/##y0osKPn///C6wEwlMPH/vykA "Japt – Try It Online")
`îQi"îQi"` transpiles to `n.î(Qi"îQi")`
```
n.î(Qi"îQi")
      "îQi"    // String "îQi"          -> îQi
    Qi         // Insert "              -> îQi"
n.î            // Repeated to length n  -> îQi"îQi"îQi"  (n=12)
```
[Answer]
# R, 203 bytes
When N = 203, the code fully print itself.
```
(f <- function(N){
str <- paste0("(f <- function(N)", paste0(as.character(body(f)), collapse = "\n"), ")}(", N, ")")
cat(rep(str, floor(N/nchar(str))), sep = "")
cat(substr(str, 1, N%%nchar(str)))})(203)
```
When N = 50, the code trims itself.
```
(f <- function(N){
str <- paste0("(f <- function(N
```
When N = 300, the code partially repeats itself.
```
(f <- function(N){
str <- paste0("(f <- function(N)", paste0(as.character(body(f)), collapse = "\n"), ")}(", N, ")")
cat(rep(str, floor(N/nchar(str))), sep = "")
cat(substr(str, 1, N%%nchar(str))))}(300)(f <- function(N){
str <- paste0("(f <- function(N)", paste0(as.character(body(f)), collapse = "\
```
[Answer]
# Matlab (57)
```
function s=f(n);s=evalc('type f');s=s(mod(1:n,nnz(s))+1);
```
The initial `1` index (instead of `0`) in the last line is because Matlab's function `type` introduces an initial line-feed, which should be removed. Thanks to Dennis for his correction (last index) and for his suggestion (`nnz` shorter than `numel`).
[Answer]
# [Unary(1-8 version)](http://esolangs.org/wiki/Talk:Unary#Inefficient_conversion_to_number), 23855 bytes
Takes input as unary of '1's, and the code is 23855 '1's (`,[.,]`)
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~40~~ 28 bytes
```
"îR+Q+V+Q+R+V"
îR+Q+V+Q+R+V
```
First time writing a quine, so this can probably be shortened quite a bit. On the other hand, I'm quite happy I got it to work at all. 
Leading newline intentional, the second line is data and the rest of it unwraps the data, then repeats the whole resulting string until it reaches length equal to the input.
Shaved off a whopping 12 bytes thanks to [Oliver](https://codegolf.stackexchange.com/users/61613/oliver).
[Try it online!](https://tio.run/##y0osKPn/n0spSDtQOwyIg7TDFApCFUpCQpW4MMX@/zc0MAAA)
[Answer]
## C++, 305
```
int L=305;string s="int main(){string t=\"string s=\";int n;cin>>n;t+=s;t+=\"\";\";t+=s;while(n>0){if(n>L){cout<<t;n-=L;}else{cout<<t.substr(0,n);}return 0;}";
int main(){string t="int L=305;string s=\"";int n;cin>>n;t+=s;t+="\";";t+=s;while(n>0){if(n>L){cout<<t;}else{cout<<t.substr(0,n);}n-=L;}return 0;}
```
**Explanation**
Apart from the escape character all other characters are printed out.
The main method is inside the string s and inside main the full string is built and printed to stdout
[Answer]
## Pyth, ~~15~~ ~~13~~ 14 bytes
```
<jN*Q]"<jN*Q]"
```
[Try it online!](https://tio.run/##K6gsyfj/3ybLTyswVglK/f9vZAkA)
Modified version of the [standard Pyth quine](https://esolangs.org/wiki/Pyth#Quine).
[Answer]
## [Hoon](https://github.com/urbit/urbit), 185 bytes
```
=/(f "=/(f k |=(n=@ =+((trim 5 f) `tape`(scag n `tape`(zing (reap n :(weld p <f> (slag 1 q))))))))" |=(n=@ =+((trim 5 f) `tape`(scag n `tape`(zing (reap n :(weld p <f> (slag 1 q))))))))
```
Set `f` to the program's code as a tape, but with "k" for itself. Split the tape at character 5, setting variables `[p=left q=right]`. Weld together the strings `p`, the original string `f`, and everything after the 1st character of `q`. Repeat that string `n` times, then return the first `n` characters of it.
Doing this was *slightly* hampered by Hoon's stdlib not having a format function or find-and-replace...Also, I'm not sure why we need another cast after the `scag`, since it should keep type information. So it goes.
[Answer]
# [Jstx](https://github.com/Quantum64/Jstx), 7 [bytes](https://quantum64.github.io/Jstx/codepage)
```
/£↕:1Fn
```
[Try it online!](https://quantum64.github.io/Jstx/JstxGWT-1.0.2/JstxGWT.html?code=L8Kj4oaVOjFGbg%3D%3D&args=MTA%3D)
[Answer]
## [Perl 5](https://www.perl.org/) with `-pa`, 48 bytes
```
$_=q{$_=substr"\$_=q{$_}:eval"x"@F",0,"@F"};eval
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3rawGkgUlyYVlxQpxUD5tVapZYk5ShVKDm5KOgY6IKrWGiT0/7@l5b/8gpLM/Lzi/7oFiQA "Perl 5 – Try It Online")
[Answer]
# [Gol><>](https://github.com/Sp3000/Golfish), 12 bytes
```
"r2ssIFLko|;
```
[Try it online!](https://tio.run/##S8/PScsszvj/X6nIqLjY080nO7/G@v9/YwMA "Gol><> – Try It Online")
### How it works
```
"r2ssIFLko|;
"..."   Push the chars in reverse order
r2ss    Reverse the stack, then push `"`
IF...|  Input n, and repeat the following n times...
  L     Push the loop counter (0 to n-1)
   k    Pop x and copy x-th from the top
    o   Pop and print as char
;       Halt
```
`k` can wrap any number of times, so we don't need to duplicate the whole stack depending on the input.
[Answer]
# SmileBASIC, ~~106~~ 66 bytes
```
INPUT N?MID$(("+CHR$(34))*3,23,N)INPUT N?MID$(("+CHR$(34))*N,23,N)
```
[Answer]
# [KSFTgolf](https://github.com/KSFTmh/KSFTgolf) - 4 chars, 6 bytes
KSFTgolf if a language I've been attempting to design for code golf. I've been changing it a lot, so this probably shouldn't actually count.
```
☃\@2
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 24 bytes
```
"34bLNca*]$("34bLNca*]$(
```
[Run and debug it](https://staxlang.xyz/#c=%2234bLNca*]%24%28%2234bLNca*]%24%28&i=3%0A28&a=1&m=2)
Adaption of the `"34bL"34bL` quine.
[Answer]
# J, 41 Bytes
Now that was a brain teaser!
```
((,quote,')$~'"_)'((,quote,'')$~''"_)')$~
```
### Explanation:
```
((,quote,')$~'"_)'((,quote,'')$~''"_)')$~  | Expression taking 1 argument
                                       $~  | Reshape left argument to fit right, taking cyclically.
(                                     )    | One large expression that evaluates to a string
                 '((,quote,'')$~''"_)'     | String literal containing the code to the left of it
 (,quote,'$)~'"_)                          | A 4-Hook:
         '$)~'"_                           | The string '$)~'
   quote,                                  | Appended to the quoted version of the string
  ,                                        | Appended to the raw string
```
Examples:
```
   ((,quote,')$~'"_)'((,quote,'')$~''"_)')$~ 10
((,quote,'
   ((,quote,')$~'"_)'((,quote,'')$~''"_)')$~ 20
((,quote,')$~'"_)'((
   ((,quote,')$~'"_)'((,quote,'')$~''"_)')$~ 30
((,quote,')$~'"_)'((,quote,'')
   ((,quote,')$~'"_)'((,quote,'')$~''"_)')$~ 41
((,quote,')$~'"_)'((,quote,'')$~''"_)')$~
   ((,quote,')$~'"_)'((,quote,'')$~''"_)')$~ 50
((,quote,')$~'"_)'((,quote,'')$~''"_)')$~((,quote,
```
[Answer]
# [Ly](https://github.com/LyricLy/Ly), 28 bytes
```
"r(34)<ns>l[p&:l,s]p<[>o<,]p
```
[Try it online!](https://tio.run/##y6n8/1@pSMPYRNMmr9guJ7pAzSpHpzi2wCbaLt9GJ7bg/38jAwA "Ly – Try It Online")
[Answer]
# Java 10, ~~193~~ 176 bytes
```
n->{var s="n->{var s=%c%s%1$c;s=s.format(s,34,s);for(int i=n;i>0;i/=176)s+=s;return s.substring(0,n);}";s=s.format(s,34,s);for(int i=n;i>0;i/=176)s+=s;return s.substring(0,n);}
```
**Explanation:**
[Try it online.](https://tio.run/##rZDBasMwEETv/ool1CARR7VJ0h6E8gfNpcfSg6I4Qam9Dtq1oQR/uyu7Kb20l5CL0O4wO7w52c4uTvuPwVWWCF6sx0sC4JHLcLCuhO04Arxy8HgEJ6ICKHVc9kl8iC17B1tAMDDgYnPpbAAys99v6lJKiwenyZA6NKG2LChbrjKSOo7TRW9Q@02u/aMpnp8kzQ3pUHIbEEhRu6MpXuRZjO5nd7s06JHh3O6qyHBF6Rq/hzr2IL6Z397BymsJn8RlrZqW1TlKXKFA5cRaTn38qf@vjM4iz2/3Rrpbveuf3D7phy8)
```
n->{                       // Method with integer parameter and String return-type
  var s="n->{var s=%c%s%1$c;s=s.format(s,34,s);for(int i=n;i>0;i/=176)s+=s;return s.substring(0,n);}";
                           //  Unformatted source code
  s=s.format(s,34,s);      //  Create the formatted quine
  for(int i=n;i>0;         //  Loop `i` as long as it's not 0
      ;                    //    After every iteration:
       i/=176)             //     int-divide `i` by the hardcoded length of the source code
    s+=s;                  //   Exponentially enlarge the source code
  return s.substring(0,n);}//  Return the first `n` characters of the source code
```
[quine](/questions/tagged/quine "show questions tagged 'quine'")-part:
* The `var s` contains the unformatted source code.
* `%s` is used to input this String into itself with the `s.format(...)`.
* `%c`, `%1$c` and the `34` are used to format the double-quotes.
* `s.format(s,34,s)` puts it all together.
Challenge part:
* `for(int i=n;i>n;i/=176)` loops `ceil(n/176)` times, where `176` is the length of the source code.
* `s+=s;` exponentially increases the size of the source code String. (`ab` becomes `abab`; `abab` becomes `abababab`; `abababab` becomes `abababababababab`; etc.)
* `s.subtring(0,n);` takes the first `n` characters of the String.
[Answer]
# [><>](https://esolangs.org/wiki/Fish), ~~22~~ 19 bytes
```
'rd3*$:?!;1-$:o}50.
```
[Try it online!](https://tio.run/##S8sszvj/X70oxVhLxcpe0dpQV8Uqv9bUQO////@6ZcYWAA "><> – Try It Online")
Takes input through the `-v` flag.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 8 bytes
```
",:m",:m
```
[Run and debug it](https://staxlang.xyz/#c=%22,%3Am%22,%3Am&i=8)
]