• Nov 19, 2022 •CodeCatch
0 likes • 1 view
public class Factorial { public static void main(String[] args) { final int NUM_FACTS = 100; for(int i = 0; i < NUM_FACTS; i++) System.out.println( i + "! is " + factorial(i)); } public static int factorial(int n) { int result = 1; for(int i = 2; i <= n; i++) result *= i; return result; } }
• Feb 6, 2021 •Daedalus
0 likes • 0 views
import java.util.Scanner; /* In Heaven, the seed is tall and the oak small / It's input vital * "The al-Madinatian," verse 236-37 * tinyurl.com/17snqkfv * email answer to [email protected] **/ public class Main { public static void main(String args[]) { int x = 0; int a, c, m = 10000; Scanner in = new Scanner(System.in); System.out.println("Please enter a seed, maximum 3000000:"); x = in.nextInt(); System.out.println("Please input a number, maximum 29:"); a = in.nextInt(); System.out.println("Please input a number, maximum 999999:"); c = in.nextInt(); char menu = 'y'; while(menu != 'n') { x = (a*x + c) % m; System.out.println("The next number is " + x + "\nWould you like another? (y/n)"); menu = in.next().charAt(0); } } } //██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗ //██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝ //██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░ //██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗ //██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝ //╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░
• Oct 7, 2023 •AustinLeath
0 likes • 5 views
import java.util.Arrays; public class LongestIncreasingSubsequence { public int lengthOfLIS(int[] nums) { if (nums.length == 0) { return 0; } int[] dp = new int[nums.length]; Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1. int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence. for (int i = 1; i < nums.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i. } } maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed. } return maxLength; } public static void main(String[] args) { LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence(); int[] nums = {10, 9, 2, 5, 3, 7, 101, 18}; int result = lis.lengthOfLIS(nums); System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4 } }
public class Daedalus extends Athenian { private Story story; public ArrayList<Skill> skills = new ArrayList<Skill>(); public ArrayList<Athenian> children = new ArrayList<Athenian>(); public Daedalus(Story story, ArrayList<Skill> inherentSkills) { if(story != null) System.out.println("ERROR No one is created with a story."); skills.add(inherentSkills); } public Momento advanceStory(Scene s) { System.err.println("ERROR Don't know how to proceed..."); } } //██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗ //██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝ //██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░ //██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗ //██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝ //╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░
import java.util.Arrays; public class QuickSortMain { private static int array[]; public static void sort(int[] arr) { if (arr == null || arr.length == 0) { return; } array = arr; quickSort(0, array.length-1); } private static void quickSort(int left, int right) { int i = left; int j = right; // find pivot number, we will take it as mid int pivot = array[left+(right-left)/2]; while (i <= j) { /** * In each iteration, we will increment left until we find element greater than pivot * We will decrement right until we find element less than pivot */ while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { exchange(i, j); //move index to next position on both sides i++; j--; } } // call quickSort() method recursively if (left < j) quickSort(left, j); if (i < right) quickSort(i, right); } private static void exchange(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String a[]){ int[] input = {33,21,45,64,55,34,11,8,3,5,1}; System.out.println("Before Sorting : "); System.out.println(Arrays.toString(input)); sort(input); System.out.println("=================="); System.out.println("After Sorting : "); System.out.println(Arrays.toString(array)); } }
import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class SimpleWordCounter { public static void main(String[] args) { try { File f = new File("ciaFactBook2008.txt"); Scanner sc; sc = new Scanner(f); // sc.useDelimiter("[^a-zA-Z']+"); Map<String, Integer> wordCount = new TreeMap<String, Integer>(); while(sc.hasNext()) { String word = sc.next(); if(!wordCount.containsKey(word)) wordCount.put(word, 1); else wordCount.put(word, wordCount.get(word) + 1); } // show results for(String word : wordCount.keySet()) System.out.println(word + " " + wordCount.get(word)); System.out.println(wordCount.size()); } catch(IOException e) { System.out.println("Unable to read from file."); } } }