자바, 165 바이트
골프 :
BigInteger f(int n){BigInteger[]a={BigInteger.ZERO,BigInteger.ONE,BigInteger.ONE};for(int i=0;i<n;){a[++i%2]=a[0].add(a[1]);a[2]=a[2].multiply(a[i%2]);}return a[2];}
이것은 BigInteger
많은 수로 인해 필요한 또 다른 경우 입니다. 그러나 텍스트 BigInteger
를 최소로 유지하면서 크기를 줄였습니다. 또한 정적 가져 오기와 비교하여 총 길이가 길어졌습니다.
이 프로그램은 배열에서 3 개의 숫자를 추적하여 작동합니다. 처음 두 개는 이전 두 피보나치 수입니다. 세 번째는 누적 된 값입니다. 루프는 다음 값을 계산하고이를 (0, 1, 0, 1, ...) 배열 인덱스로 저장하여 시작합니다. 따라서 값 비싼 (소스 크기 측면에서) 할당 작업으로 값을 전환 할 필요가 없습니다. 그런 다음 그 새로운 가치를 포착하고 그것을 누산기에 곱하십시오.
임시 객체를 피하고 루프를 두 개의 할당 연산자로 제한함으로써 꽤 많은 바이트를 짜낼 수있었습니다.
언 골프 드 :
import java.math.BigInteger;
public class Fibonacci_orial {
public static void main(String[] args) {
// @formatter:off
String[][] testData = new String[][] {
{ "1", "1" },
{ "2", "1" },
{ "3", "2" },
{ "4", "6" },
{ "5", "30" },
{ "6", "240" },
{ "7", "3120" },
{ "8", "65520" },
{ "9", "2227680" },
{ "10", "122522400" },
{ "11", "10904493600" },
{ "12", "1570247078400" },
{ "13", "365867569267200" },
{ "14", "137932073613734400" },
{ "15", "84138564904377984000" },
{ "16", "83044763560621070208000" },
{ "17", "132622487406311849122176000" },
{ "18", "342696507457909818131702784000" },
{ "19", "1432814097681520949608649339904000" },
{ "20", "9692987370815489224102512784450560000" },
{ "100", "3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000" }
};
// @formatter:on
for (String[] data : testData) {
System.out.println("Input: " + data[0]);
System.out.println("Expected: " + data[1]);
System.out.print("Actual: ");
System.out.println(new Fibonacci_orial().f(Integer.parseInt(data[0])));
System.out.println();
}
}
// Begin golf
BigInteger f(int n) {
BigInteger[] a = { BigInteger.ZERO, BigInteger.ONE, BigInteger.ONE };
for (int i = 0; i < n;) {
a[++i % 2] = a[0].add(a[1]);
a[2] = a[2].multiply(a[i % 2]);
}
return a[2];
}
// End golf
}
프로그램 출력 :
Input: 1
Expected: 1
Actual: 1
Input: 2
Expected: 1
Actual: 1
Input: 3
Expected: 2
Actual: 2
Input: 4
Expected: 6
Actual: 6
Input: 5
Expected: 30
Actual: 30
Input: 6
Expected: 240
Actual: 240
Input: 7
Expected: 3120
Actual: 3120
Input: 8
Expected: 65520
Actual: 65520
Input: 9
Expected: 2227680
Actual: 2227680
Input: 10
Expected: 122522400
Actual: 122522400
Input: 11
Expected: 10904493600
Actual: 10904493600
Input: 12
Expected: 1570247078400
Actual: 1570247078400
Input: 13
Expected: 365867569267200
Actual: 365867569267200
Input: 14
Expected: 137932073613734400
Actual: 137932073613734400
Input: 15
Expected: 84138564904377984000
Actual: 84138564904377984000
Input: 16
Expected: 83044763560621070208000
Actual: 83044763560621070208000
Input: 17
Expected: 132622487406311849122176000
Actual: 132622487406311849122176000
Input: 18
Expected: 342696507457909818131702784000
Actual: 342696507457909818131702784000
Input: 19
Expected: 1432814097681520949608649339904000
Actual: 1432814097681520949608649339904000
Input: 20
Expected: 9692987370815489224102512784450560000
Actual: 9692987370815489224102512784450560000
Input: 100
Expected: 3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000
Actual: 3371601853146468125386964065447576689828006172937411310662486977801540671138589868616500834190029067583665182291701553172011082574587431382310099030394306877775647395167143332483560925112960024644459715300507481235056111434293619038347456390454209587101225261757371666449068625033999573552165524529725467628060170886602001077137613803027158648329335507728698605769992818756765633305318529965186184043999696650407246193257877568825245646129366994079739720698147440310773871269639752334356493678913424390564535389212240038895626811627949132978086070255082668392290037141141291484839596694182152062726390364094447642643912371532491388089634845995941928089653751672688740718152064107169357399466473375804972260594768969952507346694189050233823596316467570584434128052398891223730335019092974935617029638919358286124350711360361279157416837428904150054292406756317837582840596331363581207781793070936765786629772999832857257349696094416616259974304208756997835360702840912518532683324936435856348020736000000000000000000000000