Show all divisors of a number

Hello together!

I am a complete Powershell newbie. But I am trying more and more to learn this.

I have a question regarding a task that I would like to solve but have no idea how to proceed and need support here.

I want to create a powershell script which I need to give an input (a number). And based on this number, I want to output all divisors of it (this from the smallest to the largest number). This works very well with a 12, but of course not with a 17. The input should always be greater than 1.

If it is a number like 17, it should then output the number and its remainder.

Additionally I should have the possibility to decide if I can start again from the beginning with a new number or end the script.

I really have no idea how to set this up and would appreciate any helpful comments.

Thank you very much!

wrongdongdirection,
Welcome to the forum. :wave:t4:

If this is an internship assignment or homework question we try avoid assisting with those as the first port of call should be your tutor, supervisor or teacher.

In any other case - as we do not like to deliver ready to use code or solutions on request - you may start reading the help you can find in the internet.

Such as …

https://4sysops.com/archives/do-the-math-with-powershell/

You should always read the complete help imcluding the examples to learn how to use the commands.

If you have started trying and just got stuck with your code you should share it here (formatted as code How to format code on PowerShell.org forum ) and we will try to solve your issues together with you.

Hello Olaf

Hello Olaf

First of all, thank you very much for your answer and the time!

I have a code that now does exactly what I want. But I want to be able to specify the int as input, which then passes this to the function. Unfortunately I only got as far as here

# Try to get divisor from number
function show-divisor ($n) {
    if($n -ge 2) {
        $lim = [Math]::Floor([Math]::Sqrt($n))
        $less, $greater = @(1), @()
        for($i = 2; $i -lt $lim; $i++){
            if($n%$i -eq 0) {
                $less += @($i)
                $greater = @($n/$i) + $greater
            }
        }
        if(($lim -ne 1) -and ($n%$lim -eq 0)) {$less += @($lim)}
        $($less + $greater)
    } else {@()}
}
"$(show-divisor 134)"

What I do not understand is why the variable does not output its value.

If I set the number at

"$(show-divisor 12)"

this works without problems.

But when I define a variable with read host and want to pass this, it does not work. The request comes but the output is blank.

$number = Read-Host -Prompt "Choose a number"
function show-divisor ($n) {
    if($n -ge 2) {
        $lim = [Math]::Floor([Math]::Sqrt($n))
        $less, $greater = @(1), @()
        for($i = 2; $i -lt $lim; $i++){
            if($n%$i -eq 0) {
                $less += @($i)
                $greater = @($n/$i) + $greater
            }
        }
        if(($lim -ne 1) -and ($n%$lim -eq 0)) {$less += @($lim)}
        $($less + $greater)
    } else {@()}
}
"$(show-divisor $number)"

What am I doing wrong here?

I actually don’t know what you mean. When I run your code and enter “25” at the “Read-Host” prompt I get the output “1 5”

When I enter the “28” at the “Read-Host” prompt I get the result “1 2 4 7 14”.

If I got you right this should be all you need for the beginning:

function Show-Divisor {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [INT]$Dividend
    )
    $Start = [Math]::Ceiling($Dividend / 2)
    $Result = 
    for ($i = $Start; $i -ge 2; $i--){
        if (($Dividend % $i) -eq 0) {
            $i
        }
    }
    $Result -join ', '
}
1 Like

Thank you again!

So I have saved my code (.ps1) and run my script (.\divisor.ps1).

The shell gives with the output “Choose a number”. When I then entered the number and confirmed, no output comes.

PS C:\Users\testsystem\Downloads> .\test.ps1
Choose a number: 12

And thank you for your code! I did the same procedure, but there I get no input to specify a number. Somehow I’m hitting a wall right now.

You may cast your function variable to the type [int] like this:

function show-divisor ([int]$n) {

But your logic seems faulty anyway. When you provide 8 as input you get 1 and 2. … it misses the 4! :wink:

The code I suggested is just the function declaration. When you want to run it when you call the script you have to put a function call (Show-Divisor) at the end of the script.

1 Like

I thank you incredibly much for your explanation and time. I have now learned what I need to pay attention to. And you are right! In my code it doesn’t output the 4… Must find out why. I tested your code and specified $(Show-Divisor). This works :)! But here the number 1 is always missing. I will have a closer look.

… you don’t need $() … only Show-Divisor is enough. :wink:

Since each integer is always dividable by itself and 1 I decided to not output it by design. :wink:

1 Like

Your design is top :)! I have still adjusted the number and now it also gives me the 1 with. Now I want to try to write this so that I can say “Enter a number” and then comes the output. After that, the option should come whether you want to specify another number.

$choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
while ( $true ) {
function Show-Divisor {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [INT]$Dividend
    )
    $Start = [Math]::Ceiling($Dividend / 2)
    $Result = 
    for ($i = $Start; $i -ge 1; $i--){
        if (($Dividend % $i) -eq 0) {
            $i
        }
    }
    $Result -join ', '
}
Show-Divisor
  $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
  if ( $choice -ne 0 ) {
    break
  }
}

My code now looks like this. It still looks a bit nasty with the repeat option (I want this as output from the script in the shell and not as a GUI option.) But it does its job :).

Thank you very much!

It will only be graphical when you run it in the ISE. In the PowerShell console it will be text.

You could change your logic for your While loop so that you are repeatedly asked for input until you enter a 0 (zero).

Even if I run the script normally in powershell, it opens the gui

I would have expected that to be impossible but anyway - if you don’t like that you still could change your logic to the approach I recommended in my last answer. :wink:

1 Like

Hello Olaf

I just wanted to thank you for your help and time. I learned a lot and have now finished my script the way I wanted!

Enjoy the day!