給定一個整數判斷是否為素數_Ruby程序檢查給定數字是否為素數

給定一個整數判斷是否為素數

檢查素數 (Checking prime number)

Before getting into writing the code, let us understand what exactly the prime numbers are? So that we could easily design its logic and implement it in the code. Prime numbers are those numbers which can only be divisible by itself or 1. So, we will design a code which can fulfill the property of prime numbers.

在編寫代碼之前,讓我們了解素數到底是什么? 這樣我們就可以輕松設計其邏輯并在代碼中實現它。 質數是那些只能被自身或1整除的數。因此,我們將設計一個可以滿足質數性質的代碼。

Methods used:

使用的方法:

  • puts: For giving the output as well as a message to the user.

    puts :用于向用戶提供輸出和消息。

  • gets: For taking the input from the user.

    gets :用于接受用戶的輸入。

  • .to_i: For converting strings into integers.

    .to_i :用于將字符串轉換為整數。

Operators used:

使用的運算符:

  • %: For retrieving the remainder.

    :用于檢索剩余部分。

  • ==: Used for comparing two values.

    == :用于比較兩個值。

  • < and >: These are comparison operators.

    <> :這些是比較運算符。

  • +: Generally used in the code for incrementing the loop variable.

    + :通常在代碼中用于增加循環變量。

Variables used:

使用的變量:

  • num: It is storing the user inputted integer value.

    num :存儲用戶輸入的整數值。

  • count: Initialised with 0 and used as a counter variable.

    count :以0初始化,并用作計數器變量。

Ruby代碼檢查天氣是否為素數 (Ruby code to check weather a number is prime or not)

=begin 
Ruby program to check whether the given number is 
prime or not.
=end
puts "Enter the number:"
num=gets.chomp.to_i
count=0
if (num==0)
puts "0 is not prime"
else
i=2
while(i<num)
if (num%i==0)
count+=1
end
i+=1
end
end
if count>1
puts "#{num} is not a prime number"
else
puts "#{num} is a prime number"
end

Output

輸出量

RUN 1 : 
Enter the number:
13
13 is a prime number
RUN 2:
Enter the number:
890
890 is not a prime number

Code explanation:

代碼說明:

This program checks whether the integer input is prime or not. The input is checked using the while loop and conditions. Based on the condition check the code prints the required output.

該程序檢查整數輸入是否為素數 。 使用while循環和條件檢查輸入。 根據條件檢查,代碼將打印所需的輸出。

翻譯自: https://www.includehelp.com/ruby/check-whether-the-given-number-is-prime-or-not.aspx

給定一個整數判斷是否為素數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542326.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542326.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542326.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python 正則findall右斜杠_python中正則表達式的使用

本文將介紹幾個最常用的正則符號&#xff0c;以及正則表達式的應用場景。如果說【數學表達式】刻畫的是數字的內在規律&#xff0c;那么【正則表達式】則是用來刻畫和描述字符串內在規律的表達式。記得剛接觸python時學習過slice&#xff0c;replace&#xff0c;split等方法&am…

JavaScript | 用戶定義函數的一些示例

1) Design a function, print message and assign the function to a variable and print it like a function 1)設計一個功能&#xff0c;打印消息并將該功能分配給變量&#xff0c;然后像打印功能一樣打印 <html lang"en"><head><script>functi…

網易 html5,別再想不開做H5了

寫這篇文章的時候網易噠噠《飼養手冊》H5刷屏了&#xff0c;但我們依舊不建議品牌做H5。H5作為大眾傳播工具的時代&#xff0c;已經過去了。盡管去年有很多H5曾經刷屏過&#xff0c;但在當時我們就一直跟朋友說&#xff0c;不要再嘗試H5了&#xff0c;性價比根本算不過來&#…

python打開word后再關閉再打開出錯_用Python寫了個程序調用word,運行完后再手動打開word文檔就變慢了,這是為啥?...

公司歸檔文件比較麻煩&#xff0c;于是用Python寫了個程序自動歸檔&#xff0c;運行無錯誤。但是運行完后問題就來了&#xff0c;自己手動打開word文檔時速度變得奇慢&#xff0c;打開一個文檔需要1~2min,請各位同仁幫我看看。下為源代碼#歸檔.pyimport osimport refrom win32c…

編程 mcq_MCQ | 8255 PPI(可編程外圍接口)

編程 mcqQuestion 1: How many pins does the 8255 PPI IC contains? 問題1&#xff1a;8255 PPI IC包含多少個引腳&#xff1f; 24 24 20 20 32 32 40 40 Answer: d. 40 答案&#xff1a;d。 40 Question 2: In which mode do all the Ports of the 8255 PPI work as Input…

flex 修改生成html,CSS Flex –動畫教程

如果一張圖片勝過千言萬語 —— 那么動畫呢&#xff1f; Flex 無法通過文字或靜態圖像有效地完全解釋。為了鞏固你對flex的了解&#xff0c;我制作了這些動畫演示。注意 overflow: hidden 行為類型是默認值&#xff0c;因為 flex-wrap 還未設置。為了獲得更好的想法&#xff0c…

c#c#繼承窗體_C#繼承能力問題和解答 套裝5

c#c#繼承窗體1) Which keyword is used to call a superclass constructor from child class? supertopconstbase Answer & Explanation Correct answer: 4base In C#.NET, base keyword is used to call a base class constructor from a derived class. 1)使用哪個關鍵字…

python php 網站_python php網站

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里技術人對外發布原創技術內容的最大平臺&…

陜西2021高考成績在哪查詢,2021陜西高考成績查詢入口

2021陜西高考成績查詢入口2021-05-13 19:38:37文/張敏有很多同學在關注2021年陜西高考成績的查詢方式&#xff0c;為了方便考生們查詢成績&#xff0c;小編整理了陜西高考成績查詢入口&#xff0c;希望對同學們有幫助。2021陜西高考成績查詢通道高考成績查詢過后應該做什么1、了…

Can’t Activate Reporting Services Service in SharePoint

訪問sharepoint的reporing service 的報表的時候莫名其妙的報錯&#xff1a; The requested service, http://amatltapp02:32843/1dacf49a2f7a4a6daa8db5768539893f/ReportingWebService.svc could not be activated. See the servers diagnostic trace logs for more informat…

scala python_Scala與Python | 哪種編程語言更好

scala pythonScala is a general-purpose programming language developed by Martin Odersky in 2004. Scala是Martin Odersky在2004年開發的通用編程語言。 Both Scala and Python are general purpose programming that is used in Data Science that supports Object Orie…

查找文件中每行第二個單詞_日語單詞中的長短音區別在哪里,日語長短音發音有什么規律...

日語單詞記憶長短音規律一、如果單詞的漢字在中文漢語拼音中是前鼻音&#xff0c;在日語讀音中就會帶撥音「ん」&#xff1b; 如果單詞的漢字在中文漢語拼音中是后鼻音&#xff0c;在日語讀音中就會帶有長音。例&#xff1a;専門&#xff08;zhuan men&#xff09;&#xff0d;…

SQL Server 執行計劃利用統計信息對數據行的預估原理二(為什么復合索引列順序會影響到執行計劃對數據行的預估)...

本文出處&#xff1a;http://www.cnblogs.com/wy123/p/6008477.html 關于統計信息對數據行數做預估&#xff0c;之前寫過對非相關列&#xff08;單獨或者單獨的索引列&#xff09;進行預估時候的算法&#xff0c;參考這里。  今天來寫一下統計信息對于復合索引在預估時候的計…

計算機三四級網絡技術,全國計算機等級考試四級網絡技術論述題真題3

1.(2003年)網絡安全策略設計的重要內容之一是&#xff1a;確定當網絡安全受到威脅時應采取的應急措施。當我們發現網絡受到非法侵入與攻擊時&#xff0c;所能采取的行動方案基本上有兩種&#xff1a;保護方式與跟蹤方式。請根據你對網絡安全方面知識的了解&#xff0c;討論以下…

哈密頓路徑_檢查圖形是否為哈密頓量(哈密頓路徑)

哈密頓路徑Problem Statement: 問題陳述&#xff1a; Given a graph G. you have to find out that that graph is Hamiltonian or not. 給定圖G。 您必須找出該圖是否為哈密頓量 。 Example: 例&#xff1a; Input: 輸入&#xff1a; Output: 1 輸出1 Because here is a …

京東自動下單軟件_黃牛軟件自動下單秒殺商品 警方用科技手段打擊

法制日報全媒體記者 張維定了10個鬧鐘,也搶不到一瓶茅臺&#xff1b;等了很久的iPhone新手機,打開網頁就秒沒……或許并不是因為你的手速、網速慢,而是黃牛黨在用軟件和你搶商品。近日,在“凈網2019”專項行動中,阿里安全協助江蘇省南通市公安局成功打掉了一個制作銷售黃牛軟件…

Mysql基礎之DML語句

增 1 給表的所有字段插入數據 INSERT INTO 表名 VALUES(值1&#xff0c;值2&#xff0c;值3&#xff0c;...&#xff0c;值n)&#xff1b; 2 給表的指定字段插入數據 INSERT INTO 表名(屬性1&#xff0c;屬性2&#xff0c;...&#xff0c;屬性n) VALUES(值1&#xff0c;值2&a…

河南招教考試計算機專業知識,河南教師招聘考試《計算機網絡技術基礎》知識點歸納七...

河南教師招聘考試《計算機網絡技術基礎》知識點歸納七1.ADSL是非對稱數字用戶線路&#xff0c;其下行速率為1.5&#xff5e;8 Mb/s&#xff0c;而上行速率則為16&#xff5e;640 kb/s。在一對銅雙絞線上的傳送距離可達5km左右&#xff0c;可同時上網打電話&#xff0c;互不影響…

Python程序檢查字符串是否是回文

What is palindrome string? 什么是回文字符串&#xff1f; A string is a palindrome if the string read from left to right is equal to the string read from right to left i.e. if the actual string is equal to the reversed string. 如果從左至右讀取的字符串等于…

content屬性的4種用途

content屬性瀏覽器支持情況&#xff0c;兼容到IE8瀏覽器&#xff0c;IE7及以下不支持用途一、配合:before及:after偽元素插入文本<div><p>偽元素</p> </div>p:before{content:CSS3;color:#4bb;font-weight:bold;margin-right:20px;background:#f0f0f0;…