Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-11-2024, 04:43 PM
|
#1
|
Member
Registered: Jan 2022
Posts: 58
Rep:
|
help factor calculation in bash?
hello, I have tried the factors with a comprehensible calculation method without "factor..." to calculate, the loop still works with 55 = 5 11 but at 120 = 30 4.
i have lost track, can someone help me further?
Code:
z=120; y=$z; x=$z;
until (( $[y--] < 3 )); do
until (( $x <= $y || $[$x/$y] <= $y || $[(${x}0/$y)%10] != 0 )); do
x=$[$x/$y];
echo $x $y $[$x/$y]
done; done;
echo -e "\n"$[$z/$x] $x" \n"
factor 120 # correct result = 120: 2 2 2 3 5
Last edited by blumenwesen; 08-11-2024 at 04:45 PM.
|
|
|
08-11-2024, 05:17 PM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,112
|
I read past the comprehensible calculation method... standby
Last edited by michaelk; 08-11-2024 at 05:36 PM.
|
|
|
08-12-2024, 03:45 AM
|
#3
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639
|
you can add set -xv to see how does it work. You can add some log lines, like print $x $y whatever to understand better what's going on.
I don't really understand how would your code print "2 2 2 3 5" at all.
|
|
|
08-12-2024, 04:21 AM
|
#4
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,915
|
Instead of the oldish $[ ] get used to $(( )) that is like (( )) but echos a result.
or
or
I did not debug your code. The following is a working code.
Code:
#!/bin/bash
x=${1:-120}
# Extra handling of divisor 2
until ((x % 2))
do
echo 2
((x /= 2))
done
for ((y = 3; y <= x / y; y += 2))
do
until ((x % y))
do
echo $y
((x /= y))
done
done
((x > 2)) &&
echo $x
Last edited by MadeInGermany; 08-12-2024 at 05:50 AM.
Reason: Using y <= x / y
|
|
|
08-12-2024, 04:27 AM
|
#5
|
LQ Newbie
Registered: Aug 2013
Posts: 14
Rep:
|
Factor numbers in bourne shell
Code:
z=120
d=2
while [ $(($d*$d)) -le $z ]; do
while [ $(($z%$d)) -eq 0 ]; do
echo -n $d ""
z=$(($z/$d))
done
d=$(($d+1))
done
if [ $z -ne 1 ]; then
echo -n $z
fi
echo
Last edited by marijan; 08-12-2024 at 04:41 AM.
|
|
|
08-12-2024, 12:46 PM
|
#6
|
Member
Registered: Jan 2022
Posts: 58
Original Poster
Rep:
|
Wow, sorry I only had time to reply now.
Thanks for the help.
Now I come on with my experiment, a run of several primes decomposed into factors by permutation in a superordinate space or "cube" taking each position one after the other. =D
Last edited by blumenwesen; 08-12-2024 at 12:55 PM.
|
|
|
08-13-2024, 12:11 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,639
|
Quote:
Originally Posted by blumenwesen
Wow, sorry I only had time to reply now.
Thanks for the help.
Now I come on with my experiment, a run of several primes decomposed into factors by permutation in a superordinate space or "cube" taking each position one after the other. =D
|
I don't really understand, just wanted to mention: bash is not really suitable for this, if you need higher speed you need to use another language.
|
|
|
09-08-2024, 10:23 AM
|
#8
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,325
|
|
|
|
All times are GMT -5. The time now is 03:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|