-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimesAndBricks.java
More file actions
66 lines (57 loc) · 1.82 KB
/
PrimesAndBricks.java
File metadata and controls
66 lines (57 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Another HackerRank challenge. This one was essentially two problems in one:
// 1) Count the number of primes less than or equal to a particular integer,
// 2) Count the number of configurations of 1x4 and 4x1 bricks in a 4xN space (leaving
// no holes).
// See the text of the challenge here: https://www.hackerrank.com/challenges/red-john-is-back
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class PrimesAndBricks {
// Counts the number of arrangements of bricks. Easily made more robust by replacing
// 4 with a parameter that gets passed in.
public static int brickCount(int width)
{
if(width<4)
return 1;
if(width >= 4)
return brickCount(width-4)+brickCount(width-1);
return 0;
}
// Tests if the given integer is prime
public static boolean isPrime(int num)
{
int cap = (int)Math.ceil(Math.sqrt(num));
for(int i=2; i<=cap; i++)
if(num%i == 0)
return false;
return true;
}
//Recursively counts the number of primes less than a given integer
public static int primes(int num)
{
if(num == 1)
return 0;
if(num==2)
return 1;
if(num>0)
if(isPrime(num))
{
//System.out.println(num+ " is prime");
return 1+primes(num-1);
}
else
return primes(num-1);
return 0;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int tests = input.nextInt();
for(int i=0; i<tests; i++)
{
int bricks = input.nextInt();
System.out.println(primes(brickCount(bricks)));
}
}
}