primality 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 82daf615f518ef5049d729cb88f81991d6c5325ba8acffc496ba7ba11aee1e32
4
+ data.tar.gz: '095330488bcc2cd44b5907da64198d1ef6505485c178d16c64d856353bede256'
5
+ SHA512:
6
+ metadata.gz: 6e6e0310bd6ec4d5aa7352466134e6425aa5eded2e563fd38f09b71678158a695ccca110dcb131b92717e647b3d08847dce41c00c4c4f9db481bac56f50c8769
7
+ data.tar.gz: bf1d14b91fa38e857240c4e5d6ae2c1ef180e51266584a9bb04be74a0d2437c2e85abb0a9a57252bc2889fa87bc0e82ca482b0e512eb78726780c27d265ced82
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in primarity.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ primality (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ power_assert (2.0.1)
10
+ rake (13.0.6)
11
+ test-unit (3.5.1)
12
+ power_assert
13
+
14
+ PLATFORMS
15
+ x86_64-darwin-19
16
+
17
+ DEPENDENCIES
18
+ primality!
19
+ rake (~> 13.0)
20
+ test-unit (~> 3.0)
21
+
22
+ BUNDLED WITH
23
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Tomoya Bansho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Primarity
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/primarity`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'primarity'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install primarity
22
+
23
+ ## Usage
24
+
25
+ ### on code
26
+ Before usage, write code below:
27
+ ```ruby:
28
+ require 'primarity'
29
+ ```
30
+
31
+ Also, you can test primarity of integer.
32
+ ```ruby:
33
+ 2.prime? #=> true
34
+ 3.prime? #=> true
35
+ 4.prime? #=> false
36
+ ```
37
+
38
+ Change arugument `prime?` to test primarity(default: `ortho`)
39
+ ```ruby:
40
+ 3.prime?(method="ortho")
41
+ 3.prime?(method="fermat")
42
+
43
+ 560.prime? #=> false
44
+ 560.prime?(method="fermat") #=> true
45
+ ```
46
+
47
+ ### on CLI
48
+ ```bash:
49
+ $ primarity 11
50
+ 11 is prime
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/primarity.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "primarity"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/primality ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "primality"
4
+
5
+ ::Primality.main
6
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primality
4
+ VERSION = "0.1.0"
5
+ end
data/lib/primality.rb ADDED
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "primality/version"
4
+
5
+ class InvalidNumberError < StandardError ; end
6
+ class UndefinedPrimalityTestMethod < StandardError; end
7
+
8
+ class Integer
9
+ def prime?(method='ortho')
10
+ if self <= 1
11
+ raise InvalidNumberError.new("#{self} must be 2 or more")
12
+ elsif ['ortho', 'fermat'].include?(method)
13
+ return self.public_send(method, self)
14
+ else
15
+ raise UndefinedPrimalityTestMethod.new("#{method} is not defined method")
16
+ end
17
+ end
18
+
19
+ def ortho(n)
20
+ (2...Math::sqrt(n).to_i+1).each do |value|
21
+ if n % value == 0 ; return false end
22
+ end
23
+ return true
24
+ end
25
+
26
+ def fermat(n)
27
+ if n == 2 ; return true end
28
+ if n == 3 ; return true end
29
+ (2...n).to_a.sample(n).each do |a|
30
+ if n.gcd(a) != 1 or a.pow(n - 1, n).to_i % n != 1 ; return false end
31
+ end
32
+ return true
33
+ end
34
+ end
35
+
36
+
37
+ module Primality
38
+ class Error < StandardError; end
39
+ # Your code goes here...
40
+
41
+ module_function
42
+ def main
43
+ i = ARGV[0]
44
+ if i.to_i.prime?
45
+ puts "#{i} is prime"
46
+ else
47
+ puts "#{i} is not prime"
48
+ end
49
+ end
50
+ end
data/primality.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/primality/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "primality"
7
+ spec.version = Primality::VERSION
8
+ spec.authors = ["Tomoya Bansho"]
9
+ spec.email = ["tomomail1997@gmail.com"]
10
+
11
+ spec.summary = "test primality"
12
+ spec.description = "test primality"
13
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, checkout our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: primality
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomoya Bansho
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: test primality
14
+ email:
15
+ - tomomail1997@gmail.com
16
+ executables:
17
+ - primality
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - exe/primality
29
+ - lib/primality.rb
30
+ - lib/primality/version.rb
31
+ - primality.gemspec
32
+ homepage:
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.2.32
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: test primality
55
+ test_files: []