Python Forum
Hard time with prime factors in the task.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hard time with prime factors in the task.
#1
Hey people.Im trying to do some harder python challenges to learn some new things. Given a string,and a number, I have to get an ascii value of a char, get the largest prime factor of the number and add it to the ascii value, ans also wrap-around values to a 0-127, and my code is:

def ascii_cipher(m, n): #message,number
        #Getting prime factorization of n:
        i = 2
        if i * i <= n:
            if not n % i:
                i = n//i
                if not n%(i**(1/2)):
                    i = int(i**(1/2))
        
        #Storing the int values of chars:      
        z_val = [ord(x)+i for x in m]
    
        #Getting the overflow point 
        def int_overflow(val):
            if val > 127:
                return (val % 128)
            if val < 0:
                return val % 128
            return val
        #Use the round up func:
        y_val = [int_overflow(x) for x in z_val]
        #Return the encrypted result
        return "".join(chr(x) for x in y_val)
If you have some math degree, you will most likely say that Im fkn retarded, and you will be right.
End of the story, thx for your attention.
Or maybe wait, I would be happy to understand it a bit - As far as I can tell my prime factors are wrong in 47/50 cases, but how to do it properly?
Reply
#2
To get the prime factors of a number:
  • Start with factor = 2, and all_factors = []
  • As long a number is evenly divisible by factor, divide number by factor and append factor to all_factors.
  • Increase factor by 1
  • Repeat the last two steps until number == 1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 675 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  count certain task in task manager[solved] kucingkembar 2 1,117 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  Schedule a task and render/ use the result of the task in any given time klllmmm 2 2,085 May-04-2021, 10:17 AM
Last Post: klllmmm
  How to create a task/import a task(task scheduler) using python Tyrel 7 3,707 Feb-11-2021, 11:45 AM
Last Post: Tyrel
  Having a hard time conceptualizing how to print something MysticLord 6 3,097 Sep-19-2020, 10:43 PM
Last Post: MysticLord
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,479 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 1,965 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Having a hard time combining two parts of code. Coozeki 6 3,063 May-10-2020, 06:50 AM
Last Post: Coozeki
  Hard time trying to have clean MySQL to CSV program PierreSoulier 2 2,764 Jul-20-2018, 07:52 AM
Last Post: PierreSoulier
  How hard is this? Phillips45 0 2,107 Jun-11-2018, 05:49 PM
Last Post: Phillips45

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020