Commit ac3fa229 authored by ali's avatar ali

HOMEWOR

parent ccf76145
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="0ac41bf7-7342-4614-88ef-bcd46556c4ab" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2XXm3H5akCvzyReVilK1Ph8MlXX" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;
}
}</component>
<component name="RunManager" selected="JUnit.PrimeNumberTest.testPrime">
<configuration name="PrimeNumberTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="lab1" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="PrimeNumberTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="PrimeNumberTest.testPrime" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="lab1" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="PrimeNumberTest" />
<option name="METHOD_NAME" value="testPrime" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.PrimeNumberTest.testPrime" />
<item itemvalue="JUnit.PrimeNumberTest" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="0ac41bf7-7342-4614-88ef-bcd46556c4ab" name="Changes" comment="" />
<created>1698783603257</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1698783603257</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/test/java/PrimeNumberTest.java</url>
<line>81</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>lab1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
package org.example;
import java.util.Arrays;
import java.util.List;
public class PrimeNumberExtractor {
public void extractPrimes(int start, int end, List<Integer> results) {
// boolean[] isPrim = new boolean[end];
// Arrays.fill(isPrim, true);
// for (int i = start ; i * i < end; i++)
// if (isPrim[i])
// for (int j = i * i; j < end; j += i)
// isPrim[j] = false;
//
// int count = 0;
// for (int i = start ; i < end; i++)
// if (isPrim[i]) count++;
int n = end - start + 1;
boolean[] isPrim = new boolean[end + 1];
Arrays.fill(isPrim, true);
for (int i = 2; i * i <= end; i++) {
for (int j = Math.max(i * i, (start + i - 1) / i * i); j <= end; j += i) {
isPrim[j] = false;
}
}
int count = 0;
for (int i = Math.max(start, 2); i <= end; i++) {
if (isPrim[i]) {
count++;
}
}
synchronized (results) {
results.add(count);
}
}
}
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.example.PrimeNumberExtractor;
public class PrimeNumberTest extends TestCase
{
public void testPrime ()
{
long startTime = System.currentTimeMillis();
PrimeNumberExtractor primeNumberExtractor = new PrimeNumberExtractor();
List<Integer> results = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
int numThreads =10;
int rangeSize = 100000000 / numThreads;
// Create and start worker threads
for (int i = 0; i < numThreads; i++) {
int start = i * rangeSize;
int end = (i + 1) * rangeSize;
Thread thread = new Thread(() -> primeNumberExtractor.extractPrimes(start, end, results));
thread.start();
threads.add(thread);
}
// Wait for all worker threads to finish
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
int totalCount = 0;
for (int count : results) {
totalCount += count;
}
double executionTimeSeconds = (double) executionTime / 1000.0;
System.out.println(numThreads + " threads are running....");
System.out.println("Total Number of Prime Numbers: " + (totalCount));
System.out.println("Execution Time: " + (executionTimeSeconds+0.1) + " seconds");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment