Skip to content

Commit 1cc9103

Browse files
author
James Lee
committed
add prime number problem and solution
1 parent 3c187a1 commit 1cc9103

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

in/prime_nums.text

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2 3 5 7 11 13 17 19 23 29
2+
31 37 41 43 47 53 59 61 67 71
3+
73 79 83 89 97 101 103 107 109 113
4+
127 131 137 139 149 151 157 163 167 173
5+
179 181 191 193 197 199 211 223 227 229
6+
233 239 241 251 257 263 269 271 277 281
7+
283 293 307 311 313 317 331 337 347 349
8+
353 359 367 373 379 383 389 397 401 409
9+
419 421 431 433 439 443 449 457 461 463
10+
467 479 487 491 499 503 509 521 523 541

src/main/java/com/sparkTutorial/rdd/airports/AirportsProblem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static void main(String[] args) throws Exception {
66

77
/* TODO: Create a Spark program to read the airport data from in/airports.text, find all the airports which are located in United States and output the airport's name and the city's name to out/airports.text.
88
9-
Each entry of the input file contains the following columns:
9+
Each row of the input file contains the following columns:
1010
1111
Airport ID, Name of airport, Main city served by airport, Country where airport is located, IATA/FAA code, ICAO Code, Latitude, Longitude, Altitude, Timezone, DST, Timezone in Olson format
1212
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sparkTutorial.rdd.sumOfPrimeNums;
2+
3+
public class SumOfFirst100PrimeNumbersProblem {
4+
5+
public static void main(String[] args) throws Exception {
6+
7+
/* TODO: Create a Spark program to read the first 100 prime numbers from in/prime_nums.text, print the sum of those numbers to console.
8+
9+
Each row of the input file contains 10 prime numbers separated by spaces.
10+
*/
11+
}
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.sparkTutorial.rdd.sumOfPrimeNums;
2+
3+
import org.apache.spark.SparkConf;
4+
import org.apache.spark.api.java.JavaRDD;
5+
import org.apache.spark.api.java.JavaSparkContext;
6+
7+
import java.util.Arrays;
8+
9+
public class SumOfFirst100PrimeNumbersSolution {
10+
11+
public static void main(String[] args) throws Exception {
12+
13+
SparkConf conf = new SparkConf().setAppName("primeNumbers").setMaster("local[*]");
14+
15+
JavaSparkContext sc = new JavaSparkContext(conf);
16+
17+
JavaRDD<String> lines = sc.textFile("in/prime_nums.text");
18+
19+
JavaRDD<String> numbers = lines.flatMap(line -> Arrays.asList(line.split("\\s+")).iterator());
20+
21+
JavaRDD<String> validNumbers = numbers.filter(number -> !number.equals(""));
22+
23+
String sum = validNumbers.reduce((x, y) -> Integer.valueOf(x) + Integer.valueOf(y) + "");
24+
25+
System.out.println("Sum is: " + sum);
26+
}
27+
}

0 commit comments

Comments
 (0)