Last active
January 3, 2020 06:07
-
-
Save serihiro/3a144ba2c2bc46fff8c812b1efa142ef to your computer and use it in GitHub Desktop.
1から10^6の範囲の整数に含まれる素数を昇順で並べた時の差分を表示するスクリプト
This file contains hidden or 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
--- | |
title: "distance_graph" | |
output: html_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
library(ggplot2) | |
``` | |
```{r} | |
data = read.csv("distance.csv", header=TRUE) | |
g <- ggplot(data, aes(x=seq, y=distance)) + | |
theme_bw() + | |
theme(panel.grid.minor = element_blank(),legend.title=element_blank(),legend.text=element_text(size=15), | |
axis.title.x = element_text(size=15), axis.title.y = element_text(size=15)) + | |
scale_y_continuous(breaks = seq(0, 140, by = 20)) + | |
coord_cartesian(ylim = c(0, 140), xlim=c(0, 80000)) + | |
geom_point(size=0.5) | |
ggsave(file='prime_number_distance.png', plot = g, dpi = 300, width = 6.4, height = 4.8) | |
g | |
``` | |
```{r} | |
min(data$distance) | |
median(data$distance) | |
sd(data$distance) | |
mean(data$distance) | |
max(data$distance) | |
``` |
This file contains hidden or 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
#include <bits/stdc++.h> | |
using namespace std; | |
bool is_prime(int n) { | |
if (n < 4) { | |
return true; | |
} | |
for (int i = 2; i * i <= n; ++i) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
int seq = 1; | |
int last_prime_number = -1; | |
for (int i = 2; i <= 1000000; ++i) { | |
if (is_prime(i)) { | |
if (last_prime_number != -1) { | |
cout << seq << ',' << i - last_prime_number << endl; | |
} else { | |
cout << seq << ',' << i << endl; | |
} | |
++seq; | |
last_prime_number = i; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment