找到半完美的数字 [英] Find semi perfect numbers

查看:190
本文介绍了找到半完美的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,



我有一个关于找到半完美数字的问题。半完美数字:除数字之外的任何数字的三个最大除数的总和等于该数字。 

例如: 

6的除数:6,   3,2,1  >>> 3 + 2 + 1 = 6这是半完美的$
30的除数:30,  15,10,6 ,5,3, 2,1>>> 15 + 10 + 6 = 31这不是半完美的b $ b $ 36的除数:36, 18,12,9 ,6, 4,3,2,1>>> 18 + 12 + 9 = 39这不是半完美的b $ b $ 18的除数:18,  9,6,3 ,2, 1>>> 9 + 6 + 3 = 18这是半完美的。



我想要一个组合框。在组合框中,将有4个选项" 1 2 3 4"。当我点击"1"时它将在图片框的一个阶段打印半完美的数字。当我点击"2"时它将在两个阶段打印半完美数字&bbsp
在图片框上。



我做了很多尝试,但它没有用。至少如果你给我图表,它对我来说是完美的。



谢谢......

Hi friends,

I have a question about finding the semi perfect numbers. Semi perfect number: The sum of three biggest divisors of any number apart from the number equals the that number. 
For example: 
6's divisors : 6, 3, 2, 1 >>> 3 + 2 + 1 = 6 This is semi perfect
30's divisors : 30, 15, 10, 6, 5, 3, 2, 1 >>> 15+10+6 = 31 This isn't semi perfect
36's divisors : 36, 18, 12, 9, 6, 4, 3, 2, 1 >>> 18+12+9 = 39 This isn't semi perfect
18's divisors: 18, 9, 6, 3, 2, 1 >>> 9 + 6 + 3 = 18 This is semi perfect.

I want there is a combobox. In combobox there will be 4 option " 1 2 3 4". When I click "1" it will print the semi perfect numbers in one stage on picturebox. When I click "2" it will print the semi perfect numbers in two stage 
on picturebox.

I did lots of tries but it didn't work. At least If you give me the diagram, it will be perfect for me.

Thanks...

推荐答案

试试这个。我使用了NumericUpDown控件+一个列表框:

try this. i used a NumericUpDown control + a listbox:

 

 


Public Class Form1

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        ListBox1.Items.Clear()
        Dim x As Integer = CInt(NumericUpDown1.Value)
        Dim divisors() As Integer = Enumerable.Range(1, x).Where(Function(i) x Mod i = 0).ToArray

        ListBox1.Items.Add("Divisors: " & String.Join(", ", Array.ConvertAll(divisors, Function(i) i.ToString)))

        'Figure out how many bitmasks we need... 
        'Calculated as:
        '    (2 ^ ItemCount) - 1
        Dim len As Integer = divisors.Length
        Dim calcs As Integer = CInt(Math.Pow(2, len)) - 1

        'Create our array of bitmasks... each item in the array
        'represents a unique combination from our divisors array
        Dim masks As String() = Enumerable.Range(1, calcs).Select(Function(i) Convert.ToString(i, 2).PadLeft(len, "0"c)).ToArray()

        'Spit out the corresponding calculation for each bitmask
        For Each m As String In masks
            'Get the items from our array that correspond to 
            'the on bits in our mask
            Dim incl As Integer() = divisors.Where(Function(c, i) m(i) = "1"c).ToArray()

            If incl.Count > 1 Then
                'Write out calculation and resulting sum
                ListBox1.Items.Add(String.Format("{0}={1} {2}", String.Join("+", incl.Select(Function(c) c.ToString()).ToArray()), incl.Sum(), If(incl.Sum() = x, "Semi Perfect", "")))
            End If
        Next
    End Sub

End Class

这篇关于找到半完美的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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