Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fibonacci;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- //String s1 = "656006096262142989350178033729549556642676383232336265433784409742154433804661332137484567136571199328212724757371571537299468910158925926520634267410117568569929788383355349652486489818108622969402477887141685962903374297020809706805196713227956014251877900218898169451536868965760698964216982448169886501238156520802621307753044482574180109648240541866529324983414010189670221762155494932626210505";
- //String s2 = "250572031945003272238062679321828252833825213808823514984330268366978962197341129230519532039014797883605808330899720778605308114380839942796889389428927605299078556674667262716674388794375852673649320630692741493085455843731600778404958287462585565120043104898780253688088872688246379722105843535447234778633004665956274642418148252674366749520021561386892682686265905694179065413231025238392035696";
- //System.out.println(skipStep(s1, s2));
- //System.out.println(addBig(s1, s2));
- Scanner sc = new Scanner(System.in);
- int term = 0;
- while (true) {
- String s = sc.nextLine().trim().toLowerCase();
- boolean form, bare = false;
- if (s.startsWith("f ")) {
- form = true;
- s = s.substring(s.indexOf(" ") + 1);
- } else if (s.startsWith("b ")) {
- bare = true;
- s = s.substring(s.indexOf(" ") + 1);
- }
- if (s.equals("") || s.equals("n")) {
- term++;
- } else if (s.equals("s")) {
- term += 2;
- } else if (s.startsWith("c ")) {
- String[] parts = s.split(" ");
- try {
- clip(addBig(parts[1], parts[2]).toString());
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("You must put two numbers to add!");
- }
- continue;
- } else if (s.equals("help")) {
- System.out.println("Number: Clip the nth term.");
- System.out.println("'n' or nothing: Calculate the next term.");
- System.out.println("'s': calculate the one after the next term.");
- System.out.println("'c [NUMBER] [NUMBER]': add two big numbers.");
- System.out.println("'b [NUMBER': calculate the nth term, but do not format it. Can preceed 's' or 'n'");
- continue;
- } else {
- try {
- term = Integer.parseInt(s);
- } catch (NumberFormatException e) {
- System.out.println("Invalid input! Enter 'help' for valid inputs!");
- }
- }
- if (bare) {
- clip(calc(term).toString());
- } else {
- clip(calcLine(term));
- }
- }
- }
- private static Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
- private static void clip(String s) {
- StringSelection ss = new StringSelection(s);
- c.setContents(ss, null);
- }
- public static BigInteger addBig(String number1, String number2) {
- BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
- return num1.add(num2);
- }
- public static BigInteger skipStep(String number1, String number2) {
- BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
- BigInteger firstStep = num1.add(num2);
- return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
- }
- public static BigInteger calc(int num) {
- BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
- for (int x = 3; x < num; x++) {
- num1 = num1.add(num2);
- x++;
- if (x == num) {
- return num1;
- }
- num2 = num2.add(num1);
- }
- return num2;
- } //1 1 2 3 5 8 13 21 34
- public static String calcLine(int num) {
- return "F(" + num + ") =\n\n" + calc(num);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment