如何确定给定数是否同质? [英] How to detect whether a given number is Homogeneous or not ?

查看:83
本文介绍了如何确定给定数是否同质?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同质数字是一个数字,如果我们反转其位数,则它保持不变,例如:123454321、5665、33、1.所有这些数字都是同质的.
请我需要接受用户输入的非递归解决方案.

The Homogeneous number is a number if we invert its digits it stay the same number e.g: 123454321, 5665, 33, 1 . All these numbers are Homogeneous.
Please i need non recursive solution that accepts user input.

推荐答案

同质数不是您所描述的-如果两个数字具有相同的素因数,则它们是同质的.均质对的一个示例是(6,72),它们都共享素数2和3.

您的号码是回文.如果您想算出真正的同质数,则从Wiki开始并通过Google进入算法-但请注意,这变得很复杂,因为有效的因式分解是大多数加密技术的根源.

处理回文数非常简单-非递归技术要优于递归技术,而且计算起来容易得多.

但是,由于这是您的作业,因此我不会给您任何代码!

1)编写一个返回布尔值的方法,将其命名为IsPalindrome-该方法应具有单个字符串参数
2)声明一个名为last的整数,将其设置为字符串的长度,减一-这是字符串中的最后一个字符.
3)建立一个循环. for循环是好的,从零开始,以字符串长度除以2结束,以1递增.
4)在循环中,将当前字符(由循环索引给定)与它的匹配字符(由最后一个字符减去循环索引给定)进行比较
5)如果存在差异,则说明该字符串不是回文,因此请立即返回false.
6)在循环结束时,字符串是回文,所以返回true.

完成!
Homogeneous numbers are not as you describe - two numbers are homogeneous if they have identical prime factors. An example of a homogeneous pair is (6, 72), both of which share prime factors 2 and 3.

Your numbers are palindromes. If you want to work out true homogenoeous numbers, then start with wiki and move to algorithms via Google - but be aware this gets complex, since efficient factorization is the root of most cryptography.

To process palindromic numbers is pretty simple - a non recursive technique is better than a recursive one, it is much easier to work out.

However, since this is your homework, I won''t give you any code!

1) Write a method returning a bool, call it IsPalindrome - the method should have a single string parameter
2) Declare an integer, called last, set it to the length of the string, minus one - this is the last character in the string.
3) Set up a loop. A for loop is good, start at zero, end at the string length divided by two, increment by one.
4) In the loop, compare the current character (as given by the loop index) with it''s matching character (as given by the last character minus the loop index)
5) If there are different, the string is not a palindrome, so return false immediately.
6) At the end of the loop, the string is a palindrome, so return true.

Done!


这篇关于如何确定给定数是否同质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆