0% found this document useful (0 votes)
5 views7 pages

Comp Sci A Challenges

The document contains seven programming challenges, each implemented in Java. The challenges include finding perfect numbers, identifying a specific orderly number with all digits, summing multiples of 3 and 5, finding the largest prime factor of a number, determining the largest palindrome from the product of two 3-digit numbers, finding the smallest number divisible by all numbers from 1 to 20, and calculating the sum of the digits of 100 factorial. Each challenge is presented with its corresponding code implementation.

Uploaded by

jadnachabe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Comp Sci A Challenges

The document contains seven programming challenges, each implemented in Java. The challenges include finding perfect numbers, identifying a specific orderly number with all digits, summing multiples of 3 and 5, finding the largest prime factor of a number, determining the largest palindrome from the product of two 3-digit numbers, finding the smallest number divisible by all numbers from 1 to 20, and calculating the sum of the digits of 100 factorial. Each challenge is presented with its corresponding code implementation.

Uploaded by

jadnachabe123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Challenge #1​

public class Main {​


public static void main(String[] args) {​
int sumOfPerfectNumbers = 0;

for (int number = 1; number < 1000; number++) {​


int sumOfFactors = 0;

for (int factor = 1; factor < number; factor++) {​


if (number % factor == 0) {​
sumOfFactors += factor;​
}​
}

if (sumOfFactors == number) {​
sumOfPerfectNumbers += number;​
}​
}

System.out.println(sumOfPerfectNumbers);​
}​
}​

Challenge #2​

public class Main {​


public static void main(String[] args) {​
for (long number = 1023456789; number <= 9876543210L; number++) {​
if (isOrderly(number) && hasAllDigits(number)) {​
System.out.println(number);​
break;​
}​
}​
}

public static boolean isOrderly(long number) {​


String numStr = Long.toString(number);​
for (int n = 1; n <= numStr.length(); n++) {​
long leftmost = Long.parseLong(numStr.substring(0, n));​
if (leftmost % n != 0) {​
return false;​
}​
}​
return true;​
}

public static boolean hasAllDigits(long number) {​


String numStr = Long.toString(number);​
for (char digit = '0'; digit <= '9'; digit++) {​
if (numStr.indexOf(digit) == -1) {​
return false;​
}​
}​
return true;​
}​
}
Challenge #3 code

public class Main {​


public static void main(String[] args) {​
int sum = 0;


for (int i = 1; i < 1000; i++) {​

if (i % 3 == 0 || i % 5 == 0) {​
sum += i; ​
}​
}

System.out.println(sum); ​
}​
}​




















Challenge #4​

public class Main {​


public static void main(String[] args) {​
long number = 600851475143L;​
long largestPrimeFactor = 0;

for (long factor = 2; factor <= number; factor++) {​


if (number % factor == 0 && isPrime(factor)) {​
largestPrimeFactor = factor;​
number /= factor;​
factor = 1;​
}​
}

System.out.println(largestPrimeFactor);​
}

public static boolean isPrime(long num) {​


if (num < 2) {​
return false;​
}​
for (long i = 2; i <= num / 2; i++) {​
if (num % i == 0) {​
return false;​
}​
}​
return true;​
}​
}











Challenge #5​

public class Main {​


public static void main(String[] args) {​
int maxPalindrome = 0;

for (int i = 999; i >= 100; i--) {​


for (int j = i; j >= 100; j--) {​
int product = i * j;​
if (isPalindrome(product) && product > maxPalindrome) {​
maxPalindrome = product;​
}​
}​
}

System.out.println(maxPalindrome);​
}

public static boolean isPalindrome(int number) {​


String str = Integer.toString(number);​
int len = str.length();​
for (int i = 0; i < len / 2; i++) {​
if (str.charAt(i) != str.charAt(len - 1 - i)) {​
return false;​
}​
}​
return true;​
}​
}








Challenge #6​

public class Main {​


public static void main(String[] args) {​
int number = 20;

while (true) {​
if (isDivisibleByAll(number)) {​
System.out.println(number);​
break;​
}​
number++;​
}​
}

public static boolean isDivisibleByAll(int number) {​


for (int i = 1; i <= 20; i++) {​
if (number % i != 0) {​
return false;​
}​
}​
return true;​
}​
}
















Challenge #7​

public class Main {​


public static void main(String[] args) {​
int[] digits = new int[200];​
digits[0] = 1;​
int digitCount = 1;

for (int i = 2; i <= 100; i++) {​


int carry = 0;​
for (int j = 0; j < digitCount; j++) {​
int product = digits[j] * i + carry;​
digits[j] = product % 10;​
carry = product / 10;​
}​
while (carry > 0) {​
digits[digitCount] = carry % 10;​
carry /= 10;​
digitCount++;​
}​
}

int sum = 0;​


for (int i = 0; i < digitCount; i++) {​
sum += digits[i];​
}

System.out.println(sum);​
}​
}

You might also like