-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
145 changed files
with
2,432 additions
and
2,471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,88 @@ | ||
# Assignment22 | ||
# Assignment24 | ||
|
||
A twin prime is a prime number that differs from another prime number by `2`. | ||
A positive, non-zero number n is a factorial prime if it is equal to `factorial(n) + 1` for some `n` and it is prime. | ||
|
||
A Fine array is defined to be an array in which every prime in the array has its twin in the array. | ||
Recall that `factorial(n)` is equal to `1 * 2 * ... * n-1 * n`. | ||
|
||
So `{4, 7, 9, 6, 5}` is a Fine array because `7 and `5` occurs. | ||
If you understand recursion, the recursive definition is: | ||
|
||
Note that `{4, 9, 6, 33}` is a Fine array since there are no primes. | ||
* `factorial(1) = 1` | ||
* `factorial(n) = n * factorial(n-1)` | ||
|
||
On the other hand, `{3, 8, 15}` is not a Fine array since `3` appear in the array but its twin `5` is not in the array. | ||
For example, `factorial(5) = 1 * 2 * 3 * 4 * 5 = 120`. | ||
|
||
Write a function named `isFineArray` that: | ||
Recall that a prime number is a natural number which has exactly two distinct natural number divisors: `1` and itself. | ||
|
||
* returns `1` if its array argument is a Fine array | ||
Write a method named `isFactorialPrime` which: | ||
|
||
* returns `1` if its argument is a factorial prime number | ||
* otherwise it returns `0` | ||
|
||
The signature of the method is `int isFactorialPrime(int n)` | ||
|
||
| if n is | then function returns | reason | | ||
|:-------------|:-------------|:-------------| | ||
| 2 | 1 | because 2 is prime and is equal to factorial(1) + 1 | | ||
| 3 | 1 | because 3 is prime and is equal to factorial(2) + 1 | | ||
| 7 | 1 | because 7 prime and is equal to factorial(3) + 1 | | ||
| 8 | 0 | because 8 is not prime | | ||
| 11 | 0 | because 11 does not equal factorial(n) + 1 for any n (factorial(3)=6, factorial(4)=24) | | ||
| 721 | 0 | because 721 is not prime (its factors are 7 and 103) | | ||
|
||
### Solution | ||
|
||
```java | ||
public class Assignment24 { | ||
public static void main(String[] args) { | ||
int result = isFactorialPrime(2); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(3); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(7); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(8); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(11); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(721); | ||
System.out.println(result); | ||
} | ||
|
||
static int isFactorialPrime(int n) { | ||
if (isPrime(n)) { | ||
int sum = 0; | ||
|
||
for (int i = 1; i < n && sum < n; i++) { | ||
sum = factorial(i) + 1; | ||
} | ||
|
||
if (sum == n) | ||
return 1; | ||
|
||
return 0; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int factorial(int n) { | ||
if (n == 0 || n == 1) | ||
return 1; | ||
return n * factorial(n -1); | ||
} | ||
|
||
static boolean isPrime(int n) { | ||
for (int i = 2; i < n; i++) { | ||
if (n % i == 0) | ||
return false; | ||
} | ||
|
||
return n < 0 ? false : true; | ||
} | ||
} | ||
``` |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,64 @@ | ||
# Assignment23 | ||
# Assignment25 | ||
|
||
Consider the prime number `11`. Note that `13` is also a prime and `13 – 11 = 2`. So, `11` and `13` are known as twin primes. | ||
A fancy number is a number in the sequence `1, 1, 5, 17, 61, ...`. | ||
|
||
Similarly, `29` and `31` are twin primes. So is `71` and `73`. | ||
Note that first two fancy numbers are `1` and any fancy number other than the first two is sum of the three times previous one and two times the one before that. | ||
|
||
However, there are many primes for which there is no twin, examples are `23`, `67`. A twin array is defined to an array which every prime that has a twin appear with a twin. | ||
See below: | ||
|
||
Some examples are: | ||
* `1` | ||
* `1` | ||
* `3 * 1 + 2 * 1 = 5` | ||
* `3 * 5 + 2 * 1 = 17` | ||
* `3 * 17 + 2 * 5 = 61` | ||
|
||
* `{3, 5, 8, 10, 27}`, `3` and `5` are twins and both are present | ||
* `{11, 9, 12, 13, 23}`, `11` and `13` are twins and both are present, `23` has no twin | ||
* `{5, 3, 14, 7, 18, 67}`, `3` and `5` are twins, `5` and `7` are twins, `67` has no twin | ||
Write a function named `isFancy` that: | ||
|
||
The following are NOT twin arrays: | ||
* returns `1` if its integer argument is a Fancy number | ||
* otherwise it returns `0` | ||
|
||
* `{13, 14, 15, 3, 5}` `13` has a twin prime and it is missing in the array | ||
* `{1, 17, 8, 25, 67}` `17` has a twin prime and it is missing in the array | ||
The signature of the function is `int isFancy(int n)` | ||
|
||
Write a function named `isTwin(int[] arr)` that | ||
### Solution | ||
|
||
* returns `1` if its array argument is a Twin array | ||
* otherwise it return `0` | ||
```java | ||
public class Assignment25 { | ||
public static void main(String[] args) { | ||
int result = isFancy(1); | ||
System.out.println(result); | ||
|
||
result = isFancy(5); | ||
System.out.println(result); | ||
|
||
result = isFancy(17); | ||
System.out.println(result); | ||
|
||
result = isFancy(61); | ||
System.out.println(result); | ||
|
||
result = isFancy(62); | ||
System.out.println(result); | ||
} | ||
|
||
static int isFancy(int n) { | ||
if (n == 1) | ||
return 1; | ||
|
||
int sum = 1; | ||
int n1 = 1; | ||
int n2 = 1; | ||
|
||
for (int i = 1; i < n; i++) { | ||
sum = 2 * n1 + 3 * n2; | ||
|
||
if (sum == n) | ||
return 1; | ||
|
||
n1 = n2; | ||
n2 = sum; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,60 @@ | ||
# Assignment24 | ||
# Assignment26 | ||
|
||
A positive, non-zero number n is a factorial prime if it is equal to `factorial(n) + 1` for some `n` and it is prime. | ||
Write a function named `largestPrimeFactor` that will return the largest prime factor of a number. | ||
|
||
Recall that `factorial(n)` is equal to `1 * 2 * ... * n-1 * n`. | ||
If the number is `<=1` it should return `0`. | ||
|
||
If you understand recursion, the recursive definition is: | ||
Recall that a prime number is `a number > 1` that is divisible only by `1` and itself, e.g. `13` is prime but `14` is not. | ||
|
||
* `factorial(1) = 1` | ||
* `factorial(n) = n * factorial(n-1)` | ||
The signature of the function is `int largestPrimeFactor(int n)` | ||
|
||
For example, `factorial(5) = 1 * 2 * 3 * 4 * 5 = 120`. | ||
|
||
Recall that a prime number is a natural number which has exactly two distinct natural number divisors: `1` and itself. | ||
|
||
Write a method named `isFactorialPrime` which: | ||
|
||
* returns `1` if its argument is a factorial prime number | ||
* otherwise it returns `0` | ||
|
||
The signature of the method is `int isFactorialPrime(int n)` | ||
|
||
| if n is | then function returns | reason | | ||
| if n is | return | reason | | ||
|:-------------|:-------------|:-------------| | ||
| 2 | 1 | because 2 is prime and is equal to factorial(1) + 1 | | ||
| 3 | 1 | because 3 is prime and is equal to factorial(2) + 1 | | ||
| 7 | 1 | because 7 prime and is equal to factorial(3) + 1 | | ||
| 8 | 0 | because 8 is not prime | | ||
| 11 | 0 | because 11 does not equal factorial(n) + 1 for any n (factorial(3)=6, factorial(4)=24) | | ||
| 721 | 0 | because 721 is not prime (its factors are 7 and 103) | | ||
| 10 | 5 | because the prime factors of 10 are 2 and 5 and 5 is the largest one. | | ||
| 6936 | 17 | because the distinct prime factors of 6936 are 2, 3 and 17 and 17 is the largest | | ||
| 1 | 0 | because n must be greater than 1 | | ||
|
||
### Solution | ||
|
||
```java | ||
public class Assignment24 { | ||
public class Assignment26 { | ||
public static void main(String[] args) { | ||
int result = isFactorialPrime(2); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(3); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(7); | ||
int result = largestPrimeFactor(10); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(8); | ||
result = largestPrimeFactor(6936); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(11); | ||
result = largestPrimeFactor(1); | ||
System.out.println(result); | ||
|
||
result = isFactorialPrime(721); | ||
result = largestPrimeFactor(11); | ||
System.out.println(result); | ||
} | ||
|
||
static int isFactorialPrime(int n) { | ||
if (isPrime(n)) { | ||
int sum = 0; | ||
|
||
for (int i = 1; i < n && sum < n; i++) { | ||
sum = factorial(i) + 1; | ||
} | ||
static int largestPrimeFactor(int n) { | ||
if (n <= 1) | ||
return 0; | ||
|
||
if (sum == n) | ||
return 1; | ||
int largestPrimeFactor = 2; | ||
|
||
return 0; | ||
for (int i = 2; i < n; i++) { | ||
if (n % i == 0 && isPrime(i)) { | ||
largestPrimeFactor = i; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int factorial(int n) { | ||
if (n == 0 || n == 1) | ||
return 1; | ||
return n * factorial(n -1); | ||
return largestPrimeFactor; | ||
} | ||
|
||
static boolean isPrime(int n) { | ||
for (int i = 2; i < n; i++) { | ||
if (n % i == 0) | ||
if (n % i == 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return n < 0 ? false : true; | ||
return n > 0; | ||
} | ||
} | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,44 @@ | ||
# Assignment25 | ||
# Assignment27 | ||
|
||
A fancy number is a number in the sequence `1, 1, 5, 17, 61, ...`. | ||
Write a function named `largestAdjacentSum` that iterates through an array computing the sum of adjacent elements and returning the largest such sum. You may assume that the array has at least `2` elements. | ||
|
||
Note that first two fancy numbers are `1` and any fancy number other than the first two is sum of the three times previous one and two times the one before that. | ||
The function signature is `int largestAdjacentSum(int[] a)` | ||
|
||
See below: | ||
|
||
* `1` | ||
* `1` | ||
* `3 * 1 + 2 * 1 = 5` | ||
* `3 * 5 + 2 * 1 = 17` | ||
* `3 * 17 + 2 * 5 = 61` | ||
|
||
Write a function named `isFancy` that: | ||
|
||
* returns `1` if its integer argument is a Fancy number | ||
* otherwise it returns `0` | ||
|
||
The signature of the function is `int isFancy(int n)` | ||
| if input parameters are | return | | ||
|:-------------|:-------------| | ||
| {1, 2, 3, 4} | 7 because 3+4 is larger than either 1+2 or 2+3 | | ||
| {18, -12, 9, -10} | 6 because 18-12 is larger than -12+9 or 9-10 | | ||
| {1,1,1,1,1,1,1,1,1} | 2 because all adjacent pairs sum to 2 | | ||
| {1,1,1,1,1,2,1,1,1} | 3 because 1+2 or 2+1 is the max sum of adjacent pairs | | ||
|
||
### Solution | ||
|
||
```java | ||
public class Assignment25 { | ||
public class Assignment27 { | ||
public static void main(String[] args) { | ||
int result = isFancy(1); | ||
int result = largestAdjacentSum(new int[]{1, 2, 3, 4}); | ||
System.out.println(result); | ||
|
||
result = isFancy(5); | ||
result = largestAdjacentSum(new int[]{18, -12, 9, -10}); | ||
System.out.println(result); | ||
|
||
result = isFancy(17); | ||
result = largestAdjacentSum(new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1}); | ||
System.out.println(result); | ||
|
||
result = isFancy(61); | ||
System.out.println(result); | ||
|
||
result = isFancy(62); | ||
result = largestAdjacentSum(new int[]{1, 1, 1, 1, 1, 2, 1, 1, 1}); | ||
System.out.println(result); | ||
} | ||
|
||
static int isFancy(int n) { | ||
if (n == 1) | ||
return 1; | ||
|
||
int sum = 1; | ||
int n1 = 1; | ||
int n2 = 1; | ||
|
||
for (int i = 1; i < n; i++) { | ||
sum = 2 * n1 + 3 * n2; | ||
|
||
if (sum == n) | ||
return 1; | ||
static int largestAdjacentSum(int[] a) { | ||
int sum = 0; | ||
|
||
n1 = n2; | ||
n2 = sum; | ||
for (int i = 0; i < a.length - 1; i++) { | ||
if (a[i] + a[i + 1] > sum) { | ||
sum = a[i] + a[i + 1]; | ||
} | ||
} | ||
|
||
return 0; | ||
return sum; | ||
} | ||
} | ||
``` |
File renamed without changes.
Oops, something went wrong.