ruby打印_Ruby程序打印數字的力量

ruby打印

Ruby中數字的冪 (Power of a number in Ruby)

The task to develop a program that prints power of a number in Ruby programming language.

開發可以用Ruby編程語言打印數字冪的程序的任務。

If we want to calculate the power of a number manually then we have to multiply the base to itself by exponent times which means that if the base is 3 and the exponent is 4, then power will be calculated as

如果要手動計算數字的冪,則必須將底數乘以自身乘以指數時間,這意味著如果底數為3且指數為4 ,則冪將計算為

power = 3*3*3*3, which will result in 81.

power = 3 * 3 * 3 * 3 ,結果為81。

Let us put the above logic into codes. We have used two methods to calculate power, one is by using user-defined function “pow” and one is with the help of ** operator. If you want to write code from scratch then the first method is appropriate for you.

讓我們將以上邏輯放入代碼中。 我們使用了兩種方法來計算功率,一種方法是使用用戶定義的函數“ pow”,另一種方法是借助**運算符。 如果您想從頭開始編寫代碼,則第一種方法適合您。

Methods used:

使用的方法:

  • puts: This is used to create an interaction with the user by putting some message on the console.

    puts :用于通過在控制臺上放置一些消息來與用戶進行交互。

  • gets: This is used to take input from the user in the form of string.

    gets :用于接收用戶以字符串形式的輸入。

  • to_i: This method is used to convert any type into an integer type.

    to_i :此方法用于將任何類型轉換為整數類型。

  • pow: This is a user-defined function which takes two arguments and returns an integer value. It is defined purposefully for calculating power by taking base and exponent as an argument.

    pow :這是一個用戶定義的函數,它帶有兩個參數并返回一個整數值。 它的定義是有目的的,以底數和指數為參數來計算功效。

Ruby代碼來計算數字的冪 (Ruby code to calculate power of a number)

=begin
Ruby program to calculate power of a number.	
=end
def pow(a,b)
power=1
for i in 1..b
power=power*a
end
return power
end
puts "Enter Base:-"
base=gets.chomp.to_i
puts "Enter exponent:-"
expo=gets.chomp.to_i
puts "The power is #{pow(base,expo)}"

Output

輸出量

RUN 1:
Enter Base:-
3
Enter exponent:-
5
The power is 243
RUN 2:
Enter Base:-
2
Enter exponent:-
3
The power is 8

Method 2:

方法2:

=begin
Ruby program to calculate power of a number 
using ** operator.	
=end
puts "Enter Base:-"
base=gets.chomp.to_i
puts "Enter exponent:-"
expo=gets.chomp.to_i
power=base**expo
puts "The power is #{power}"

Output

輸出量

RUN 1:
Enter Base:-
5
Enter exponent:-
5
The power is 3125
RUN 2:
Enter Base:-
9
Enter exponent:-
2
The power is 81

翻譯自: https://www.includehelp.com/ruby/print-power-of-a-number.aspx

ruby打印

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

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

相關文章

二、訓練fashion_mnist數據集

一、加載fashion_mnist數據集 fashion_mnist數據集中數據為28*28大小的10分類衣物數據集 其中訓練集60000張,測試集10000張 from tensorflow import keras import tensorflow as tf import matplotlib.pyplot as plt import numpy as npfashion_mnist keras.data…

jquerymobile 切換頁面時候閃爍問題

https://github.com/jquery/jquery-mobile/commit/acbec71e29b6acec6cd2087e84e8434fecc0053f 可以修改css好像是個bug -4,9 4,10 * Dual licensed under the MIT (MIT-LICENSE.txt) or GPL (GPL-LICENSE.txt) licenses.*/.spin {--webkit-animation-name: spin;--webkit-an…

二分法:兩個有序數組長度為N,找到第N、N+1大的數

題目 兩個有序數組長度為N,找到第N、N1大的數 思路1:雙指針,O(N)復雜度 簡述思路: 如果當前A指針指向的數組A的內容小于B指針指向的數組B的內容,那么A指針往右移動,然后nums(當前已經遍歷過的數字個數)也…

Javascript -- In

http://www.caveofprogramming.com/articles/javascript-2/javascript-in-using-the-in-operator-to-iterate-through-arrays-and-objects/ http://msdn.microsoft.com/en-us/library/ie/9k25hbz2(vvs.94).aspx轉載于:https://www.cnblogs.com/daishuguang/p/3392310.html

三、自動終止訓練

有時候,當模型損失函數值預期的效果時,就可以結束訓練了,一方面節約時間,另一方面防止過擬合 此時,設置損失函數值小于0.4,訓練停止 from tensorflow import keras import tensorflow as tf import matplo…

矩陣形狀| 使用Python的線性代數

Prerequisite: Linear Algebra | Defining a Matrix 先決條件: 線性代數| 定義矩陣 In the python code, we will add two Matrices. We can add two Matrices only and only if both the matrices have the same dimensions. Therefore, knowing the dimensions o…

[數據庫]oracle客戶端連服務器錯誤

昨天晚上和今天上午用11g客戶端連同事10g服務器,報錯: The Network Adapter could not establish the connection 檢查嘗試了好多次都沒好。 用程序連,依舊是報這個錯,所以一查就解決了! 參考:http://apps…

ASP.NET 抓取網頁內容

(轉)ASP.NET 抓取網頁內容 ASP.NET 抓取網頁內容-文字 ASP.NET 中抓取網頁內容是非常方便的,而其中更是解決了 ASP 中困擾我們的編碼問題。 需要三個類:WebRequest、WebResponse、StreamReader。 WebRequest、WebRespo…

leetcode 53. 最大子序和 動態規劃解法、貪心法以及二分法

題目 給定一個整數數組 nums ,找到一個具有最大和的連續子數組(子數組最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4] 輸出: 6 解釋: 連續子數組 [4,-1,2,1] 的和最大,為 6。 進階: 如果你…

四、卷積神經網絡(Convolution Neural Networks)

一、CNN(Convolution Neural Networks) 卷積神經網絡基本思想:識別物體的特征,來進行判斷物體 卷積Convolution:過濾器filter中的數值與圖片像素值對應相乘再相加,6 * 6卷積一次(步數為1)變成4 * 4 Max Pooling:對卷積…

POJ3096Surprising Strings(map)

題意:輸入很多字符串,以星號結束。判斷每個字符串是不是“Surprising Strings”,判斷方法是:以“ZGBG”為例,“0-pairs”是ZG,GB,BG,這三個子串不相同,所以是“0-unique”…

vs助手使用期過 編譯CEGUI的問題:error C2061: 語法錯誤: 標識符“__RPC__out_xcount_part” VS2010...

第一個問題,下一個破解版的VX_A.dll,將其覆蓋以前的dll即可, 但是目錄有所要求,如下: XP系統:系統盤\Documents and Settings\用戶名\Local Settings\Application win7或者vistaData\Microsoft\VisualStud…

五、項目實戰---識別人和馬

一、準備訓練數據 下載數據集 validation驗證集 train訓練集 數據集結構如下: 將數據集解壓到自己選擇的目錄下就行 最后的結構效果如下: 二、構建模型 ImageDataGenerator 真實數據中,往往圖片尺寸大小不一,需要裁剪成一樣…

leetcode 122. 買賣股票的最佳時機 II 思考分析

目錄題目貪心法題目 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個算法來計算你所能獲取的最大利潤。你可以盡可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多筆交易(你必…

css設置a連接禁用樣式_使用CSS禁用鏈接

css設置a連接禁用樣式Question: 題: Links are one of the most essential aspects of any web page or website. They play a very important role in making our website or web page quite responsive or interactive. So the topic for discussion is quite pe…

服務器出現 HTTP 錯誤代碼,及解決方法

HTTP 400 - 請求無效 HTTP 401.1 - 未授權:登錄失敗 HTTP 401.2 - 未授權:服務器配置問題導致登錄失敗 HTTP 401.3 - ACL 禁止訪問資源 HTTP 401.4 - 未授權:授權被篩選器拒絕 HTTP 401.5 - 未授權:ISAPI 或 CGI 授權失敗 HTTP 40…

leetcode 55. 跳躍游戲 思考分析

題目 給定一個非負整數數組,你最初位于數組的第一個位置。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 判斷你是否能夠到達最后一個位置。 示例1: 輸入: [2,3,1,1,4] 輸出: true 解釋: 我們可以先跳 1 步,從位置 0 到達 位置 1…

六、項目實戰---識別貓和狗

一、準備數據集 kagglecatsanddogs網上一搜一大堆,這里我就不上傳了,需要的話可以私信 導包 import os import zipfile import random import shutil import tensorflow as tf from tensorflow.keras.optimizers import RMSprop from tensorflow.kera…

修改shell終端提示信息

PS1:就是用戶平時的提示符。PS2:第一行沒輸完,等待第二行輸入的提示符。公共設置位置:/etc/profile echo $PS1可以看到當前提示符設置例如:顯示綠色,并添加時間和shell版本export PS1"\[\e[32m\][\uyou are right…

java 字謎_計算字謎的出現次數

java 字謎Problem statement: 問題陳述: Given a string S and a word C, return the count of the occurrences of anagrams of the word in the text. Both string and word are in lowercase letter. 給定一個字符串S和一個單詞C ,返回該單詞在文本…